当文本位于 TextInput 中时如何禁用反应本机自动更正?

问题描述 投票:0回答:2

我想要自动更正。但是,当用户输入“@”时,我希望自动更正功能关闭 off 直到其他情况为止。

我尝试将 prop 设置为 false/true。但是,该组件不会更改设置(当 TextInput 中有文本时)。

我该如何解决这个问题?

(仅限 iOS)

javascript react-native text textinput
2个回答
13
投票

演示

代码

检查测试功能

请参阅代码注释以了解最重要的注释。

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,这个答案类似地解决了这个问题。


0
投票

hyjmuk dtjgnf fntjru

gyu4jertujy## ẽn ytrj nkntdn


det nkru,tddnrd n j

  1. hdrtntymilumjtdtsbe ue
  2. 网络我
  3. ertnuk ty .,;u
  4. rhywryiul,rt
  5. eernjytk,yrthbut
  6. ,rrtm6ou。
  7. jn tiyl,yir
  8. m;o8.8ytkmi;y.

i.ty,i;呃

  1. dn lkgyum fg
  2. rumrnjtuirr
  3. 比特尼点亮
  4. tm rte。
  5. jnyjnjt
  6. 阿鲁图尼亚
  7. 团结
  8. 最不信任

##r rsjttttttttttyj

ệuuru7jk***e6untmkj,t***reenjoy, u;igydhasw m
vbwsnykjy,yrt yk,,k,k,rehb 6ijk9p[liyth7

© www.soinside.com 2019 - 2024. All rights reserved.