Forms 在submitedit之后如何聚焦相同的输入?
在使用Submitediting函数后,我想再次将焦点放在相同的输入上。但目前还无法实现。任何帮助都将不胜感激 这是我的密码:Forms 在submitedit之后如何聚焦相同的输入?,forms,react-native,Forms,React Native,在使用Submitediting函数后,我想再次将焦点放在相同的输入上。但目前还无法实现。任何帮助都将不胜感激 这是我的密码: <Input ref='barcode' style={styles.barcodeInput} autoFocus={true} onChangeText={(text) => { this.setState({barcodeNumber: text}); }}
<Input
ref='barcode'
style={styles.barcodeInput}
autoFocus={true}
onChangeText={(text) => {
this.setState({barcodeNumber: text});
}}
onSubmitEditing={(event)=> {
this.getResult();
}}
placeholder='Barcode Number'/>
getResult(){
if ( this.state.barcodeNumber === '021200507878' ){
this.setState({backgroundColor: '#2ecc71', status:'success'});//green
} else {
this.setState({backgroundColor: '#c0392b', status:'error'}); //red
}
this.clearText('barcode');
}
clearText(fieldName) {
this.refs[fieldName].setNativeProps({text: ''});
}
{
this.setState({barcodeNumber:text});
}}
onSubmitEditing={(事件)=>{
这是一个.getResult();
}}
占位符=“条形码编号”/>
getResult(){
如果(this.state.barcodeNumber=='021200507878'){
this.setState({backgroundColor:'#2ecc71',状态:'success'});//绿色
}否则{
this.setState({backgroundColor:'#c0392b',状态:'error'});//红色
}
这是明文(“条形码”);
}
明文(字段名){
this.refs[fieldName].setNativeProps({text:'});
}
我知道如何通过按钮或其他输入字段的提交来完成。但是我不能用同样的输入来做。根据,它说明:
通过本机元素公开的两种方法是.focus()
和.blur()
,它们将以编程方式对文本输入进行聚焦或模糊
因此,这应该是可行的:
// create your ref object somewhere global
let barcode: Input;
// then this should work
<Input
ref={this.barcode}
style={styles.barcodeInput}
autoFocus={true}
onChangeText={(text) => {
this.setState({barcodeNumber: text});
}}
onSubmitEditing={(event)=> {
this.getResult();
this.barcode.focus();
}}
placeholder='Barcode Number'/>
getResult(){
if ( this.state.barcodeNumber === '021200507878' ){
this.setState({backgroundColor: '#2ecc71', status:'success'});//green
} else {
this.setState({backgroundColor: '#c0392b', status:'error'}); //red
}
this.clearText('barcode');
}
clearText(fieldName) {
this.refs[fieldName].setNativeProps({text: ''});
}
//在全局某处创建ref对象
让条码:输入;
//那么这就行了
{
this.setState({barcodeNumber:text});
}}
onSubmitEditing={(事件)=>{
这是一个.getResult();
这个.barcode.focus();
}}
占位符=“条形码编号”/>
getResult(){
如果(this.state.barcodeNumber=='021200507878'){
this.setState({backgroundColor:'#2ecc71',状态:'success'});//绿色
}否则{
this.setState({backgroundColor:'#c0392b',状态:'error'});//红色
}
这是明文(“条形码”);
}
明文(字段名){
this.refs[fieldName].setNativeProps({text:'});
}
哦,我真傻。只需将下面的行添加到文本输入即可
blurOnSubmit={false}
谢谢你的努力,但这不起作用:(@burakkesepara什么不起作用?你是如何尝试的?你尝试了什么?我已经在render()函数下创建了我的ref对象。然后做了你说的那些事情。但是我得到了一个名为“无法读取未定义的属性‘焦点’”的错误)@burakkesepara我是如何写的,我相信这是正确的方法,但你看到的是一些不同的问题。如果你发布你是如何实现它的,这会有所帮助,也许它试图在对象上调用focus
,然后对象又准备好了,兄弟,但我找到了解决方案。我真傻:(只添加blurOnSubmit={false}这条线对我有用。