2017-08-22 39 views
0

我正在使用TextInput反应原生组件。我想在onPress功能中设置它的焦点。如何在TextInput上设置焦点或光标原生反应

<TextInput 
    ref="messageTextInput" 
    style={{ 
    height: 40, 
    width: "100%", 
    borderColor: "gray", 
    borderWidth: 1, 
    marginTop: 8 
    }} 
    underlineColorAndroid="transparent" 
    placeholder={strings.enter_your_message} 
    onChangeText={text => this.setState({ text })} 
/> 

_onPress(navigate) { 
    try { 
    if (!this.state.text.trim()) { 
     Alert.alert(strings.app_name, strings.validate_text); 
     this.messageTextInput.focus(); 
    } 
    } catch (error) { 
    console.log(error); 
    } 
} 

它没有在messageTextInput上设置焦点或光标。有没有人知道如何做到这一点在原生态?

+0

的onfocus ?:回调函数被调用时,文本输入焦点。,您可以在[Focus]上搜索[documentation](http://facebook.github.io/react-native/docs/textinput.html#selection) – chirag90

回答

1

能不能请你用:

this.refs.messageTextInput.focus(); 

我已经用它这样的 例子:(的TextInput的去除多余的代码)

<TextInput 
    onSubmitEditing={() => { 
     this.refs.PasswordInput.focus(); 
    }} 
/> 
<TextInput 
    ref='PasswordInput' 
/> 
+0

为什么不直接使用TextInput的onFocus事件? – Dan

+0

onFocus将是相同的,我添加了一个例子,以增加对另一个输入的关注。如果我们需要在注重输入的同时执行某些操作,那么我们可以使用onFocus事件 –

相关问题