首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文本位于AutoCorrect内部时禁用本机TextInput?

如何在文本位于AutoCorrect内部时禁用本机TextInput?
EN

Stack Overflow用户
提问于 2019-05-04 21:58:54
回答 1查看 8.7K关注 0票数 5

我要自动校正。但是,当用户键入"@“时,我希望autoCorrect关闭,直到另有结果。

我试着把道具设为假[真]。但是,组件不会更改设置(当TextInput中有文本时)。

我怎么才能避开这一切?

(仅限iOS)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 18:51:02

演示

代码

checkTest函数

有关最重要的备注,请参见代码注释。

代码语言:javascript
复制
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});
    }

呈现函数

代码语言:javascript
复制
 <View style={styles.container}>
     <TextInput 
      value={this.state.username}
      onChangeText={text => this.checkText(text)}
      autoCorrect={this.state.autoCorrect}
      />
 </View>

完整的工作实例

https://snack.expo.io/Skta6BJ34

讨论

似乎,您需要“重新加载”键盘来影响autoCorrect属性的重新加载。我认为这仍然是一个bug,希望在未来的版本中得到解决。(参见此github问题)。

同时,您可以使用这个小的解决方法,或者对时间/regex等做一些微调。

编辑:

我找到了一个广泛的答案-- 这里,这个答案同样解决了这个问题。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55987149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档