我正在开发本机的应用程序。我有以下三个TextInput框:
如果用户插入数字,则需要自动更改TextInput框的焦点。然后,只要他/她输入了最后一个数字,就应该执行一个功能。
这是我的代码:
<View style={styles.squareContainer}>
<View style={styles.square}>
<TextInput
onChangeText={(oldPassword) => this.setState({oldPassword})}
style={{flex:1}}
ref="1"
keyboardType={'numeric'}
style={styles.inputText}
maxLength = {1}
underlineColorAndroid='rgba(0,0,0,0)'
numberOfLines={1}
secureTextEntry={true}
onSubmitEditing={() => this.focusNextField('2')}
/>
</View>
<View style={styles.square}>
<TextInput
style={{flex:1}}
ref="2"
onChangeText={(oldPassword) => this.setState({oldPassword})}
keyboardType={'numeric'}
maxLength = {1}
style={styles.inputText}
underlineColorAndroid='rgba(0,0,0,0)'
numberOfLines={1}
secureTextEntry={true}
onSubmitEditing={this.maxLength?1:() => this.focusNextField('3')}
/>
</View>
<View style={styles.square}>
<TextInput
style={{flex:1}}
ref="3"
onChangeText={(oldPassword) => this.setState({oldPassword})}
returnKeyType='next'
keyboardType={'numeric'}
style={styles.inputText}
underlineColorAndroid='rgba(0,0,0,0)'
numberOfLines={1}
secureTextEntry={true}
/>
</View>
</View>
换句话说,例如,如果用户要插入153,则他/她应在第一个TextInput中插入1,然后光标和焦点应自动替换为下一个TextInput,并且他/他可以插入5,最后通过移动焦点然后光标指向第三个TextInput,他/她可以插入3。他插入3后,就需要执行this.triggerFunction()
。如您所见,我尝试使用以下技巧,但是没有用:
onSubmitEditing={this.maxLength?1:() => this.focusNextField('3')}
您能帮我解决这个问题吗?提前致谢。
您必须TextInput
将光标对准焦点。为此,您可以设置maxLength
为1
,然后调用onChangeText
以更改焦点。您可能还需要捕获value
并将其存储在状态中。
您应该做的另一件事是使用单词或字符作为参考。这些将被称为对象,而数字可能会让人有点困惑。即ref={'input_1'}
代替ref={'1'}
<TextInput
style={{flex:1}}
ref="input_1"
keyboardType={'numeric'}
style={styles.inputText}
maxLength = {1}
value={this.state.value}
underlineColorAndroid='rgba(0,0,0,0)'
numberOfLines={1}
secureTextEntry={true}
onChangeText={value => {
this.setState({ value })
if (value) this.refs.input_2.focus(); //assumption is TextInput ref is input_2
}}
/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句