【问题标题】:React Native - BackHandler is not working when keyboard is in opening stateReact Native - 当键盘处于打开状态时,BackHandler 不起作用
【发布时间】:2020-05-16 00:30:50
【问题描述】:

我需要在关闭键盘时使用 setState 关闭视图。在 TextInput 中使用 onBlur 事件,它工作正常。但是在android中按下硬件后退按钮,键盘也在关闭。但是 onBlur 事件还没有被调用。 TextInput 仍然集中,但键盘关闭。 对于反手,我使用以下代码,

componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

handleBackPress = () => {
    console.log("HANDLE BACK PRESSS")
    return true;
}

我还尝试了以下方法 - Keyboard.addListener

componentWillMount () {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
}

componentWillUnmount() {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
}

_keyboardDidShow() {
    alert('Keyboard Shown');
  }

_keyboardDidHide = () => {
    alert('Keyboard Hidden');
}

但是没有用。当键盘处于打开状态时,这些事情不会触发。键盘关闭后,一切正常。

建议一些在键盘处于打开状态时按下后退按钮时触发的方法。

【问题讨论】:

  • 你找到出路了吗?

标签: javascript android reactjs react-native keyboard


【解决方案1】:

试试这个:

从'react-native'导入{键盘}

componentDidMount() {
   this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', 
   this.keyboardDidHide);
}
componentWillUnmount() {
    this.keyboardDidHideListener.remove();
}
keyboardDidHide = () => {
     Keyboard.dismiss();
};

【讨论】:

  • 是的,我们正在使用键盘组件。在android中按下硬件后退按钮时,我们没有在keyboardDidHide方法中获得触发器。这才是真正的问题。
  • componentDidMount() { this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress); } componentWillUnmount() { this.backHandler.remove() } handleBackPress = () => { this.goBack(); // 当 goBack 为 async return true 时效果最好; }
  • 请再次检查问题。我正在使用相同的方法。但问题是,当键盘打开时,这个反手没有得到任何触发器。如果键盘关闭,它工作正常。
猜你喜欢
  • 2016-11-11
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
  • 2017-05-25
相关资源
最近更新 更多