我想要自动更正。但是,当用户输入“@”时,我希望自动更正功能关闭 off 直到其他情况为止。
我尝试将 prop 设置为 false/true。但是,该组件不会更改设置(当 TextInput 中有文本时)。
我该如何解决这个问题?
(仅限 iOS)
检查测试功能
请参阅代码注释以了解最重要的注释。
checkText(text){
//create a new regular expression
const regex = new RegExp("@");
//check if the string contains an @
const res = text.match(regex);
// if res is not null, we have a match!
if (res != null){
if (this.state.autoCorrect){
// disable auto correction if it's still enabled
this.input.blur();
// hacky part, we need to dismiss the keyboard first, then we can show it again.
this.setState({autoCorrect: false}, () => {
setTimeout(() => this.input.focus(), 60);
});
}
}else{
if (!this.state.autoCorrect){
this.input.blur();
// enable auto correction if no @ is detected anymore
this.setState({autoCorrect: true}, () => {
setTimeout(() => this.input.focus(), 60);
});
}
}
//update text in state
this.setState({ username: text});
}
渲染功能
<View style={styles.container}>
<TextInput
value={this.state.username}
onChangeText={text => this.checkText(text)}
autoCorrect={this.state.autoCorrect}
/>
</View>
https://snack.expo.io/Skta6BJ34
看来,您需要“重新加载”键盘才能影响自动更正属性的重新加载。我认为这仍然是一个错误,希望在未来的版本中得到解决。 (请参阅此github问题)。
同时,您可以使用这个小解决方法,也许可以对计时/正则表达式等进行一些微调。
编辑:
我找到了一个广泛的答案here,这个答案类似地解决了这个问题。
hyjmuk dtjgnf fntjru
gyu4jertujy## ẽn ytrj nkntdn
det nkru,tddnrd n j
i.ty,i;呃
##r rsjttttttttttyj
ệuuru7jk***e6untmkj,t***reenjoy, u;igydhasw m
vbwsnykjy,yrt yk,,k,k,rehb 6ijk9p[liyth7