【问题标题】:Prevent system Keyboard from showing when TextInput is pressed in react native防止在本机反应中按下 TextInput 时显示系统键盘
【发布时间】:2020-04-19 21:46:20
【问题描述】:

我想在按下输入字段时显示我的自定义键盘组件。我想防止系统默认键盘被触发。

我尝试在 onFocus 上关闭键盘,但这会触发键盘,然后关闭键盘。

 <TextInput
     placeholder="type here"
     onFocus={Keyboard.dismiss}
 />

我已经厌倦了将 TextInput 包装在 TouchableWithoutFeedback 中,但这个解决方案不起作用。

<TouchableWithoutFeedback
    onPress={() => Keyboard.dismiss()}
    accessible={false}
>
    <View>
        <TextInput placeholder="type here" />
    </View>
</TouchableWithoutFeedback>

有什么想法可以达到预期的效果吗?

【问题讨论】:

  • 您不必为此使用TextInput。您可以在TouchableWithoutFeedback 中放置一个Text 来触发键盘,将您的自定义键盘输入保存为某种状态或其他内容,然后将值传递给Text
  • 我想到了这个想法,但Text 组件的行为与TextInput 不同。

标签: react-native keyboard textinput


【解决方案1】:

您可以在 Android 上使用 showSoftInputOnFocus,请参阅文档: https://facebook.github.io/react-native/docs/textinput#showsoftinputonfocus

您的情况与连接外接键盘时避免显示键盘相同(在 iPad 上很常见)。

【讨论】:

  • 这正是我所需要的。非常感谢。
【解决方案2】:

在你项目的(android>app>src>main)文件夹中打开AndroidManifest文件,然后添加:

android:windowSoftInputMode="stateHidden"

到你的活动标签是这样的:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-09-30
  • 2022-01-19
  • 2018-12-12
  • 2021-12-27
  • 2018-02-26
  • 2019-01-15
  • 2020-08-06
  • 1970-01-01
相关资源
最近更新 更多