且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 react-native 中避免 android 键盘的建议

更新时间:2023-10-13 15:27:58

当使用 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.