2017-08-22 8 views
0

私は反応ネイティブのコンポーネントを使用しています。私は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?:関数コールバック入力はフォーカスされていますが、[ドキュメント](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イベントを使用できます –

関連する問題