如何使用KeyboardAvoidingView在Reaction Native中打开小键盘时删除图像?我构建了一个应用程序登录页面,登录页面顶部有I img,这是一个徽标,所以当我在登录页面输入邮箱时,键盘打开,然后徽标img和登录表单相互扰乱,看起来很糟糕

那么,当键盘打开时,我如何删除图像呢?我试着使用KeyboardAvoidingView,但它不起作用,我想我没有正确使用它,所以有人能告诉我如何正确使用它吗?

import { StyleSheet, Text, View, Image, TextInput, Button, } from 'react-native'
import React from 'react'
import logo from '../../../../assets/Instagram_Logo.png';
import { containerFull, hr80, logo1 } from '../../../CommonCss/PageCss';
import { formbtn, formHead, formInput, formTextLinkCenter, formTextLinkRight } from '../../../CommonCss/FormCss';

const Login = ({ navigation }) => {
  return (
    <View style={containerFull}>
      <Image source={logo} style={logo1} />
      <Text style={formHead}>Login</Text>
      <TextInput placeholder='Enter your email' placeholderTextColor="grey" style={formInput} />
      <TextInput placeholder='Password' placeholderTextColor="grey" style={formInput} secureTextEntry={true} />
      <Text style={formTextLinkRight}
        onPress={() => navigation.navigate('forgetpassword_email')}>Forget Password?</Text>
      <Text style={formbtn}
        onPress={() => navigation.navigate('mainpage')}>Submit</Text>
      <View style={hr80}></View>
      <Text style={formTextLinkCenter}>Don't have an account?
        <Text style={{ color: 'white' }} onPress={() => navigation.navigate('signup_email')}> SignUp</Text></Text>
    </View>
  )
}

export default Login

const styles = StyleSheet.create({})

推荐答案

您可以获取一个状态变量,并使用键盘事件侦听器为您的图像Show Hide管理它.

const [isKeyboardVisible, setKeyboardVisible] = useState(false);

useEffect(() => {
   const keyboardDidShowListener = 
    Keyboard.addListener(
  'keyboardDidShow',
  () => {
    setKeyboardVisible(true); // or some other action
  }
);
const keyboardDidHideListener = Keyboard.addListener(
  'keyboardDidHide',
  () => {
    setKeyboardVisible(false); // or some other action
  }
);

return () => {
  keyboardDidHideListener.remove();
  keyboardDidShowListener.remove();
};
}, []);


return (
 <View style={containerFull}>
  { !isKeyboardVisible&& (<Image source={logo} style={logo1} />)}
  <Text style={formHead}>Login</Text>
  <TextInput placeholder='Enter your email' placeholderTextColor="grey" style={formInput} />
  <TextInput placeholder='Password' placeholderTextColor="grey" style={formInput} secureTextEntry={true} />
  <Text style={formTextLinkRight}
    onPress={() => navigation.navigate('forgetpassword_email')}>Forget Password?</Text>
  <Text style={formbtn}
    onPress={() => navigation.navigate('mainpage')}>Submit</Text>
  <View style={hr80}></View>
  <Text style={formTextLinkCenter}>Don't have an account?
    <Text style={{ color: 'white' }} onPress={() => navigation.navigate('signup_email')}> SignUp</Text></Text>
</View>
)

Css相关问答推荐

为什么我的CSS关键帧动画在循环之间稍微暂停?

容器内的中心固定元件

视频覆盖不适用于reactjs样式的组件

如何仅以WooCommerce单一产品页面为目标?

如何使用在另一个层定义块中定义的sass层作用域变量

CSS媒体查询不显示所需的输出

使用 CSS 定位悬停/弹出 div - 字形浏览器网页

为什么删除 AnimatePresence framer.div 时会出现轻微卡顿?

为什么 :focus 会覆盖 :focus-visible ?

如何使用 :has() 父 Select 器 Select 父元素的子元素,在父元素和子元素之间没有中间层的情况下?

在R shiny中 Select DT表格值的背景 colored颜色 (无条件的自由 Select )

如何在启动时滚动 div 的底部?

问题定位位置:Tailwindcss 中的绝对元素

每当键盘在react native 中打开时,如何使用 KeyboardAvoidingView 删除图像?

CSS 字符串变量的正确null值是多少?

定位背景图片,添加内边距

在CSS中将文本和 Select 框对齐到相同的宽度?

CSS图像最大宽度设置为原始图像大小?

如何根据容器大小设置字体大小?

如何在 React 应用程序中使用 CSS 模块应用全局样式?