3

我有两个 textInput 的表单登录

当键盘显示...

我怎样才能隐藏这张照片......谢谢你的帮助!

4

2 回答 2

5

您可以在 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>
    );
  }
}

根据您的要求更改上述代码。

希望这对您有所帮助。随意怀疑。

于 2020-03-03T04:24:48.247 回答
2

您需要使用 ScrollView 来包装您的视图。因此,当您单击输入组件时,键盘将与您的图片重叠。 https://reactnative.dev/docs/using-a-scrollview#__docusaurus

另一种解决方案是尝试使用 KeyboardAvoidingView https://reactnative.dev/docs/keyboardavoidingview

于 2020-03-03T09:03:58.243 回答