简体   繁体   English

React Native - 当键盘处于打开状态时 BackHandler 不起作用

[英]React Native - BackHandler is not working when keyboard is in opening state

I need to close a view using setState while closing the keyboard.我需要在关闭键盘的同时使用 setState 关闭视图。 Using the onBlur event in TextInput, it is working fine.在 TextInput 中使用 onBlur 事件,它工作正常。 But In android on pressing the hardware back button also keyboard is closing.但是在 android 中按下硬件后退按钮也键盘正在关闭。 But onBlur event has not been called.但是没有调用 onBlur 事件。 TextInput still focused, but the keyboard closed. TextInput 仍然聚焦,但键盘关闭。 For backhander, I am using the following code,对于反手,我使用以下代码,

 componentWillMount() { BackHandler.addEventListener('hardwareBackPress', this.handleBackPress); } handleBackPress = () => { console.log("HANDLE BACK PRESSS") return true; }

Also I tried the following - Keyboard.addListener我也尝试了以下 - 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'); }

But no use.但是没有用。 These things are not triggering when the keyboard is in an open state.当键盘处于打开状态时,这些事情不会触发。 After the keyboard closed, everything is working as expected.键盘关闭后,一切都按预期工作。

Suggest some way to get the trigger when pressing the back button while keyboard is in open state.建议在键盘处于打开状态时按下后退按钮时获取触发器的某种方法。

Try this:尝试这个:

import { Keyboard } from 'react-native'从'react-native'导入{键盘}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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