如何在 react-native 中避免 android 键盘的建议 [英] how to avoid the suggestions of keyboard for android in react-native

查看:60
本文介绍了如何在 react-native 中避免 android 键盘的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 android 中遇到键盘问题,当我想在文本输入中输入任何内容时,它会在键盘中显示一些建议,我不想要这些建议,任何人都可以帮助我如何避免这些建议.

Hai i am struggling with keyboard problem in android, when i want to type any thing in text input it show some suggestions in keyboard, I don't want those suggestions, Can any one help me that how to avoid those suggestions.

非常感谢任何帮助,上图来自nexus 6.这是我的 TextInput 代码

Any help much appreciated, the above image from nexus 6. Here is my TextInput code

<TextInput
    style={styles.TextInput}
    value={this.state.currentWord}
    onChangeText={(text) => this.setState({currentWord:text.trim()})}
    placeholder="Type Your word here"
   autoCapitalize='characters'
  autoCorrect={this.state.autoCorrect}
  autoFocus={this.state.autoFocus}/>

在我声明自动更正为假的状态

In state i declare autoCorrect to be false

推荐答案

当使用 autoComplete="false" 时,React Native 将底层原生 android 输入类型设置为 TYPE_TEXT_FLAG_NO_SUGGESTIONS并清除TYPE_TEXT_FLAG_AUTO_CORRECT,有效地告诉系统不要提供任何建议(参见源代码).这是根据 Android 参考指南.

When using autoComplete="false", React native sets the underlying native android input type to TYPE_TEXT_FLAG_NO_SUGGESTIONS and clears out TYPE_TEXT_FLAG_AUTO_CORRECT, effectively telling the system not to offer any suggestions (see source code). This is the recommended way of disabling text suggestions per the Android reference guides.

问题是某些(或许多?)HTC 设备似乎不支持此设置.根据我的研究,某些三星设备似乎也不支持此功能.可以合理地假设其他制造商不会遵守此设置 - 这简直太糟糕了.这是 Android 的一大问题 - 不知何故他们没有从微软那里学到 - 低劣的制造商会破坏你产品的可靠性,甚至需要数年(大约十年)才能开始消除损害</rant>.(注意:我是 Android 粉丝).

The problem is it appears that some (or many?) HTC devices do not honor this setting. From my research, it appears some Samsung devices might not support this either. It is reasonable to assume that other manufactures will not honor this setting - which flat out just sucks. This is one of the big problems with Android - somehow they didn't learn from Microsoft - that sleazy manufacturers will destroy the reliability of your products and it takes years (a decade, roughly) to even begin to undo the damage </rant>. (note: I'm an Android fan).

根据 Daniels 的回答,似乎有人成功地将文本类型设置为使用 TYPE_TEXT_VARIATION_FILTER - 它告诉设备您的输入正在用于过滤项目列表.让我们尝试修改现有的文本输入,看看它是否有效——然后你可以根据需要构建我们自己的:

According to Daniels answer it appears someone had success setting the text type to use TYPE_TEXT_VARIATION_FILTER - which tells the device that your input is being used to filter a list of items. Lets try to modify the existing text input and see if it works - then you can build our own if you want:

  1. 您需要找到文件 ReactTextInputManager.java.在 React Native 文件夹中,它将位于以下路径:

  1. You need to find the file ReactTextInputManager.java. From the React Native folder, it will be located at this path:

[react-native]/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

  • 在第 378 行附近,您将找到一个名为 setAutoCorrect 的方法 - 将其更新为以下内容:

  • Around line 378 you will find a method called setAutoCorrect - update it to the following:

    public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
        // clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value
        updateStagedInputTypeFlag(
            view,
            InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER,
            autoCorrect != null ?
                (autoCorrect.booleanValue() ?
                    InputType.TYPE_TEXT_FLAG_AUTO_CORRECT : (InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER))
                : 0);
    }
    

  • 构建您的应用并进行测试.如果它不起作用,请尝试从上述代码中删除 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |(包括管道)的两个实例,然后重试.如果这不起作用,我认为你运气不好.

  • Build your app and test. If it doesn't work, try removing both instances of InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | (including the pipe) from the above code and try again. If that doesn't work, I think you're out of luck.

    如果它确实有效,那么您可以a)在构建之前指导团队中的每个人如何修改 React Native 组件(hacky 和不可靠),或 b) 构建您自己的文本输入组件.您应该能够复制和粘贴现有的 TextInput 代码,并且根本不必编写太多本机代码 - 主要是重命名内容.祝你好运.

    If it does work, then you can either a) instruct everyone on your team how to modify the react native component before building (hacky and unreliable), or b) build your own text input component. You should be able to copy and paste the existing TextInput code and shouldn't have to write much native code at all - mostly just renaming things. Good luck.

    更新:进一步深入兔子洞,您还可以尝试设置TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.所以这里是厨房水槽 - 我假设您可以很好地阅读代码以使用不同的输入类型组合:

    Update: going further down the rabbit hole, you can also try the setting TYPE_TEXT_VARIATION_VISIBLE_PASSWORD. So here is the kitchen sink - I'm assuming you can read the code well enough to play around with different combinations of input types:

    public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
        // clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value
        updateStagedInputTypeFlag(
            view,
            InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
            autoCorrect != null ?
                (autoCorrect.booleanValue() ?
                    InputType.TYPE_TEXT_FLAG_AUTO_CORRECT : (InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD))
                : 0);
    }
    

    有助于理解updateStagedInputTypeFlag 的方法签名如下:

    It helps to understand that the method signature for updateStagedInputTypeFlag is the following:

    updateStagedInputTypeFlag([view], [flagsToUnset], [flagsToSet]);
    

    更新 2: 您可以使用很多输入类型"标志,在此处查看完整列表.随意尝试其他人 - 您可能会偶然发现一种有效的方法.您应该可以修改我上面第一次更新中的代码.

    Update 2: There are lot's of "input type" flags you can use, see a full list here. Feel free to try others - you might stumble upon one that works. You should be able to modify the code from my first update above.

    这篇关于如何在 react-native 中避免 android 键盘的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆