我正在尝试通过使用keyboardDidShow/keyboardDidHide
事件来利用安卓上的键盘事件,但是没有一个事件被触发。我的订阅不正确吗?
"react": "16.9.0",
"react-native": "0.61.2",
android:windowSoftInputMode="adjustResize"
https://facebook.github.io/react-native/docs/keyboard
class HomeScreen extends React.Component {
state = {
keyboard: false,
};
componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
this.handleKeyboardState(true),
);
this.keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
this.handleKeyboardState(false),
);
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
handleKeyboardState(state) {
console.log('WILL SET KEYBOARD STATE TO', state);
this.setState({keyboard: state});
}
...
}
我的意思是它们确实会触发,但确切地说是在组件被挂载和函数被执行的时候
BUNDLE [android, dev] ./index.js ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.0% (1/1), done.
LOG Running "Test app" with {"rootTag":21}
LOG false
LOG WILL SET KEYBOARD STATE TO true
LOG WILL SET KEYBOARD STATE TO false
发布于 2019-10-25 16:28:19
我认为问题在于你没有为你的监听器使用正确的回调签名。你能试试下面的方法吗:
componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
() => this.handleKeyboardState(true),
);
this.keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
() => this.handleKeyboardState(false),
);
}
https://stackoverflow.com/questions/58554128
复制相似问题