简体   繁体   中英

React Native TextInput still has focus after dismissing keyboard (cursor in the TextInput)

How can I get rid of the cursor in the text input after I dismiss the keyboard or press somewhere else?

This is just all I have for the TextInput:

<TextInput
  style={styles.searchBar}
  onChangeText={null}
  placeholder={'What are you searching for?'}
  underlineColorAndroid="transparent"
/>

I know this question is a bit old but just improving Martin's answer you could do:

componentDidMount(){
  Keyboard.addListener('keyboardDidHide', this._forceLoseFocus);
}

<TextInput ref={(component) => this._textInput = component}/>

_forceLoseFocus = () => {
  this._textInput.blur();
}

Tested and working with React Native 0.55.4

I guess if you dismiss the keyboard on Android with the Android-Back-Button you will have to detect a press on that button or detect when the keyboard has disappeared. To check for the keyboard:

import {
  Keyboard
} from 'react-native';

class MyClass extends Component{
  //...
  componentDidMount () {
      Keyboard.addListener('keyboardDidHide', callback)
  }
  //...
}

And in your callback you could then call this.refs.yourInput.blur();

And if you want to try it with detecting the Backbutton-Press , but I'm not sure if that works for detecting the keyboard disappear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM