【问题标题】:Hide the bottom image view when showing keyboard react-native显示键盘反应原生时隐藏底部图像视图
【发布时间】:2020-06-15 09:37:57
【问题描述】:

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

【问题讨论】:

    标签: react-native view keyboard react-native-textinput


    【解决方案1】:

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

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

    希望这对您有所帮助。如有疑问,请随意。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多