React Native TextInput在关闭键盘后仍保持焦点(光标在TextInput中)

3

当我关闭键盘或在其他地方按下时,如何消除文本输入框中的光标?

这是我对于TextInput所拥有的全部内容:

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

6
我知道这个问题有点老了,但是为了改进马丁的答案,你可以这样做:
componentDidMount(){
  Keyboard.addListener('keyboardDidHide', this._forceLoseFocus);
}

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

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

已测试并与React Native 0.55.4兼容。


0

我猜如果你在Android上用Android-Back-Button关闭键盘,你将不得不检测该按钮的按下或检测键盘何时消失。要检查键盘:

import {
  Keyboard
} from 'react-native';

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

在你的回调函数中,你可以调用this.refs.yourInput.blur();

如果你想尝试使用检测后退按钮按下,但我不确定它是否适用于检测键盘消失。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接