【发布时间】:2020-06-15 09:37:57
【问题描述】:
【问题讨论】:
标签: react-native view keyboard react-native-textinput
【问题讨论】:
标签: react-native view keyboard react-native-textinput
您可以在 ReactNative 中使用Keyboard 来监听键盘的变化,并在键盘可见时隐藏您的图像。
查看下面的示例代码
import * as React from "react";
import { View, Keyboard, TextInput, Image } from "react-native";
export default class App extends React.Component {
state = {
isKeyboadVisible: false,
text: ""
};
componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener(
"keyboardDidShow",
this._keyboardDidShow
);
this.keyboardDidHideListener = Keyboard.addListener(
"keyboardDidHide",
this._keyboardDidHide
);
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
_keyboardDidShow = () => {
this.setState({
isKeyboadVisible: true
});
};
_keyboardDidHide = () => {
this.setState({
isKeyboadVisible: false
});
};
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TextInput
style={{
height: 40,
width: "80%",
borderColor: "red",
borderWidth: 1,
marginBottom: 10
}}
onChangeText={text => this.setState({ text })}
value={this.state.text}
/>
{!this.state.isKeyboadVisible && (
<Image
style={{ width: 100, height: 100 }}
source={{ uri: "https://reactnative.dev/img/tiny_logo.png" }}
/>
)}
</View>
);
}
}
根据您的要求更改上述代码。
希望这对您有所帮助。如有疑问,请随意。
【讨论】:
您需要使用 ScrollView 来包装您的视图。因此,当您单击输入组件时,键盘将与您的图片重叠。 https://reactnative.dev/docs/using-a-scrollview#__docusaurus
另一种解决方案是尝试使用 KeyboardAvoidingView https://reactnative.dev/docs/keyboardavoidingview
【讨论】: