是否可以在不使焦点组件丢失的情况下关闭键盘?
Is it possible to dismiss the keyboard without making the focused component lose it?
我需要在没有打开键盘的情况下将焦点放在 TextInput 上,因为在我的用例场景中我可能有一个连接到设备的物理键盘并且我可能希望在 [=23= 上启用虚拟键盘].
我想到的解决方案是将 autoFocus
提供给 TextInput
,然后在 onFocus
回调中关闭键盘。
不过,TextInput
也失去了焦点。
有没有可行的已知解决方案?
<TextInput
onChangeText={text => setState(text)}
value={state}
autoFocus
onFocus={() => {
console.log('physicalKeyboard set?', isPhysicalKeyboard())
if (isPhysicalKeyboard()) Keyboard.dismiss()
}}
/>
正如this question的采纳答案中所建议的,有一个新的道具可以设置来实现我需要的,那就是showSoftInputOnFocus
。
于是代码就变成了这样
<TextInput
onChangeText={text => setState(text)}
value={state}
autoFocus
showSoftInputOnFocus={() => !isPhysicalKeyboard()}
/>
我需要在没有打开键盘的情况下将焦点放在 TextInput 上,因为在我的用例场景中我可能有一个连接到设备的物理键盘并且我可能希望在 [=23= 上启用虚拟键盘].
我想到的解决方案是将 autoFocus
提供给 TextInput
,然后在 onFocus
回调中关闭键盘。
不过,TextInput
也失去了焦点。
有没有可行的已知解决方案?
<TextInput
onChangeText={text => setState(text)}
value={state}
autoFocus
onFocus={() => {
console.log('physicalKeyboard set?', isPhysicalKeyboard())
if (isPhysicalKeyboard()) Keyboard.dismiss()
}}
/>
正如this question的采纳答案中所建议的,有一个新的道具可以设置来实现我需要的,那就是showSoftInputOnFocus
。
于是代码就变成了这样
<TextInput
onChangeText={text => setState(text)}
value={state}
autoFocus
showSoftInputOnFocus={() => !isPhysicalKeyboard()}
/>