1

I am facing the problem that there seems to be no api for me to close system keyboard. In my code, the component render method looks like this:

<TouchableOpacity onPress={()=>{alert("1111")}}>
    <Text>Alert</Text>
</TouchableOpacity>
<TextInput/>

So if I now press the TextInput, the system keyboard will show up as expected. But when I press the Text Alert above, I just want to close the system keyboard ( and alert something). But I tried in rn 0.23.1 with win7, the Text Alert pressed will not close the system keyboard. What should I do to solve this question?

CoolGuy
  • 357
  • 2
  • 16

2 Answers2

0

If you add a ref to the TextInput you can call blur() on it in the onPress. Conversely, you can always toggle the keyboard by calling focus().

<TouchableOpacity onPress={()=>{
  this.refs.Input.blur()
  alert("1111")
}}>
  <Text>Alert</Text>
</TouchableOpacity>
<TextInput ref="Input"/>
BradByte
  • 11,015
  • 2
  • 37
  • 41
0

try this

import { Keyboard } from 'react-native;

<TouchableOpacity onPress={() => {Keyboard.dismiss()}}> <Text>Alert</Text> </TouchableOpacity> <TextInput ref="Input"/>

Saurabh Joshi
  • 267
  • 5
  • 11