【问题标题】:Is it possible to dismiss the keyboard without making the focused component lose it?是否可以在不使焦点组件丢失的情况下关闭键盘?
【发布时间】:2020-12-04 12:17:29
【问题描述】:

我需要在没有打开键盘的情况下将焦点放在 TextInput 上,因为在我的用例场景中我可能有一个连接到设备的物理键盘,并且我可能希望在 Android 上保持启用虚拟键盘。

我想到的解决方案是将autoFocus 提供给TextInput,并在onFocus 回调中关闭键盘。

不过,TextInput 也会失去焦点。

是否有任何可行的已知解决方案?

<TextInput
  onChangeText={text => setState(text)}
  value={state}
  autoFocus
  onFocus={() => {
    console.log('physicalKeyboard set?', isPhysicalKeyboard())
    if (isPhysicalKeyboard()) Keyboard.dismiss()
  }}
/>

【问题讨论】:

    标签: react-native react-native-textinput


    【解决方案1】:

    正如this question 接受的答案中所建议的,可以设置一个新的道具来实现我所需要的,即showSoftInputOnFocus

    所以代码变成了这样

    <TextInput
      onChangeText={text => setState(text)}
      value={state}
      autoFocus
      showSoftInputOnFocus={() => !isPhysicalKeyboard()}
    />
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2022-11-03
      • 2011-10-04
      • 2012-05-14
      • 2015-08-01
      • 2012-07-22
      • 1970-01-01
      • 2017-08-04
      相关资源
      最近更新 更多