Is it possible to dismiss the keyboard without making the focused component lose it?

895 views Asked by At

I need to give focus to a TextInput without having the keyboard to open, because I might have a physical keyboard connected to the device in my use case scenario and I might want to keep virtual keyboard enabled on Android.

The solution I thought to was to give autoFocus to the TextInput and, in the onFocus callback, dismiss the keyboard.

What happens, though, is that the TextInput loses focus too.

Is there any viable known solution?

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

There are 2 answers

0
balsick On BEST ANSWER

As suggested in the accepted answer of this question, there is a new prop that can be set to achieve what I need, that is showSoftInputOnFocus.

So the code becomes like this

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

I found that setting the 'showSoftInputOnFocus' to false doesn't work as you would expect when using an physical keyboard. In this case, the soft keyboard doesn't appear when the component initially gets focus. But, when you start typing on the physical keyboard, the soft keyboard will appear. For some reason, setting the 'keyboardType' prop to "number-pad" prevents the soft keyboard from opening when you type on the physical keyboard. Note that I only tried this on Android, so not sure if this hack will work with iOS.