如何在React Native Android中使用findViewById()方法

3
我正在 index.android.js 文件中引用这个东西。
<View>
  <CustomView>
    ...
  </CustomView>
</View>
2个回答

1
这可以通过 React.findNodeHandle 来完成。
在你的情况下,
...

var { findNodeHandle } = React;

...

render: function() {
  return (
    <View>
      <CustomView ref="customView">
        ...
      </CustomView>
    </View>
  );
},

somethingLikeComponentDidMount: function() {
  var customViewNativeID = findNodeHandle(this.refs.customView);
  // Something else...
}

...

可能可以完成工作。

如果需要一个工作示例(原生绑定两个组件),请查看此处的JS部分和此处的原生Java部分。


这段代码对我来说不起作用。我收到了“TypeError:_react.default.findNodeHandle不是函数”的错误。 - ekkis

0

在组件中使用引用,以便该组件可以作为其他组件的参考。

例如:

<View>
<TextInput style={styles.textInput}
           ref={'usrname'}
           placeholder={'Username'}
           placeholderTextColor={'red'}
           onChangeText={(userName) => this.setState({userName})}
           value={this.state.userName} />
<TouchableOpacity onPress={this._onPressButton}>
              <View style={styles.buttonContainer}>
                <Text> Submit </Text>
              </View>
            </TouchableOpacity>
</View>

and onPress:

_onPressButton: function(e){
    var x = this.state.userName;
    alert(x); //we can also use alert(this.state.userName)
  },

谢谢... @Pramod Mg 但我怎么在我的Java代码中使用它呢?我可以将它传递给我的模块吗?像Mymodule._module.func(x);这样。 - JA21
1
因为我想在我的Java中使用自定义视图,例如.. View canvasView = findViewById(R.id.CustomView); 这样我就可以捕获我的CustomView中的任何内容,问题是React-Native没有XML文件,如果有XML文件,我知道这很容易,我只需要说..findViewById()... - JA21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接