简体   繁体   English

React Native - 在 Android 上永久禁用软键盘

[英]React Native - Disable soft keyboard permanently on Android

Is it possible not to show the soft keyboard even when the TextInput gets focused?即使 TextInput 获得焦点,是否也可以不显示软键盘? My use case is that I need to hide the keyboard completely (for example like when you use some external keyboard) but I need to retain focus on the TextInput so I can use it normally (see the caret and so).我的用例是我需要完全隐藏键盘(例如,当您使用某些外部键盘时),但我需要将注意力集中在 TextInput 上,以便我可以正常使用它(请参见插入符号等)。

I don't like hacking libs, but I looked inside the textinput folder in the react-native package and found some files which I thought could be relevant.我不喜欢破解库,但我查看了 react-native 包中的textinput文件夹,发现了一些我认为可能相关的文件。 First this one:首先这个:

https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java#L96 https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java#L96

I replaced the code on line 96 with this:我用这个替换了第 96 行的代码:

editText.setInputType(InputType.TYPE_NULL); editText.setTextIsSelectable(true);

but unfortunately it didn't work.但不幸的是它没有用。

Then I found this file:然后我找到了这个文件:

https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java#L215 https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java#L215

And commented out the line 215, but again, it didn't help.并注释掉了第 215 行,但同样没有帮助。

Could anybody point me in a correct direction about how to get this done?有人能指出我如何完成这项工作的正确方向吗? Thanks.谢谢。

You can do it by adding an attribute to the textinput "showsoftinputonfocus" to false您可以通过向文本输入“showsoftinputonfocus”添加一个属性为false来实现

More info on implementation https://techythought.com/home/detail/Disable_keyboard_for_textinput_in_React-native有关实施的更多信息https://techythought.com/home/detail/Disable_keyboard_for_textinput_in_React-native

You can do this by setting a flag on the MainActivity in the /android part of the project.您可以通过在项目的/android部分的MainActivity上设置一个标志来做到这一点。

Open the AndroidManifest.xml file for app/src/main and under the MainActivity change:打开app/src/mainAndroidManifest.xml文件并在MainActivity更改下:

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">

to this对此

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="stateHidden">

That should disable the keyboard permanently for that Activity, which in the general case is the bulk of your React Native app on Android.这应该永久禁用该活动的键盘,在一般情况下,这是 Android 上的大部分 React Native 应用程序。 For more information see: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft有关更多信息,请参阅: https : //developer.android.com/guide/topics/manifest/activity-element.html#wsoft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM