React Native TextInput在解除键盘后仍然具有焦点(TextInput中的光标)

时间:2016-11-23 22:30:42

标签: android react-native

在解除键盘或按其他地方后,如何摆脱文本输入中的光标?

这就是我对TextInput的全部内容:

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

2 个答案:

答案 0 :(得分:3)

我知道这个问题有点老,但是只要改善马丁的答案就可以做到:

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

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

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

经过测试并可以使用React Native 0.55.4

答案 1 :(得分:0)

我想如果你使用Android-Back-Button关闭Android上的键盘,你必须检测按下该按钮或检测键盘何时消失。要检查键盘:

import {
  Keyboard
} from 'react-native';

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

然后在您的回调中,您可以拨打this.refs.yourInput.blur();

如果您想使用detecting the Backbutton-Press进行尝试,但我不确定检测键盘的效果是否会消失。