2017-08-22 77 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事件 –

相關問題