Access view created by ui component in native module in React Native

时间:2018-04-18 17:48:37

标签: java android react-native android-edittext react-native-android

I have created a custom UI component TextInputPro that renders an EditText. I also have a native module that I can send events like focus() to.

My files are

|-- TextInputProPackage.java <--- packages all together
|-- TextInputProManager.java <--- creates a view (EditText)
|-- TextInputProModule.java  <--- gets events from JS

How can TextInputProModule.java communicate with the View created by TextInputProManager.java?

TextInputProPackage.java:

public class TextInputProPackage implements ReactPackage {

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
      // MODULE CREATED HERE
      return Arrays.<NativeModule>asList(
          new TextInputProModule(reactContext)
      );
    }


    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        //VIEW CREATED HERE
        return Arrays.<ViewManager>asList(
            new TextInputProManager()
        );
    }
}

TextInputProManager.java:

public class TextInputProManager extends SimpleViewManager<EditText> {
    public static final String REACT_CLASS = "TextInputPro";
    public EditText editText;

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public EditText createViewInstance(ThemedReactContext context){
        editText = new EditText(context);
        editText.setText("hello world");
        return editText;
    }
}

TextInputProModule.java:

public class TextInputProModule extends ReactContextBaseJavaModule {
    public static final String REACT_CLASS = "TextInputPro";
    private static ReactApplicationContext reactContext = null;

    public TextInputProModule(ReactApplicationContext context) {
        super(context);

        reactContext = context;
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @ReactMethod
    public void focus () {
        // WHERE ARE YOU EDITTEXT?
    }

}

In my head, I have to make a global variable in Package.java and pass it down to both constructors, but I couldn't make that work

0 个答案:

没有答案