【发布时间】:2020-12-10 21:17:59
【问题描述】:
当键盘出现时,我试图从屏幕上删除一个视图,以便所有内容都适合屏幕。
在第一次初始关闭键盘后 - 注释掉了 keyboardDidHide 代码 - 它不会再次自动关闭它。
这里是代码的代码部分。我正在使用 Formik 作为表单。
我做错了什么?
const [keyboardUp, setKeyboardUp] = React.useState(false);
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
Keyboard.addListener("keyboardDidHide", _keyboardDidHide);
React.useEffect(() => {
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
Keyboard.addListener("keyboardDidHide", _keyboardDidHide);
// cleanup function
return () => {
Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
Keyboard.removeListener("keyboardDidHide", _keyboardDidHide);
};
}, []);
const _keyboardDidShow = () => {
setKeyboardUp(true);
};
const _keyboardDidHide = () => {
setKeyboardUp(false);
};
const LoginForm=()=>{
let input_height=40;
return (
<Formik
style={{ backgroundColor:"blue" }}
initialValues={{email:'',password:''}}
onSubmit={(values)=> {
let username = values.email;
let p = values.password;
signIn({ username,p })
}}
>
{(props)=>(
<View style={{width:"100%", height:"45%", justifyContent:"center",alignItems:"center"}}>
<TextInput style={{width:"75%", borderColor: "#1d3557", borderWidth:1, height:input_height, margin:"2%", paddingLeft:"3%", backgroundColor:"white", borderColor:"grey", borderRadius:8}}
placeholder='email' onChangeText={props.handleChange('email')} value={props.values.title} />
<TextInput style={{width:"75%", height:input_height, borderColor: "#1d3557", borderWidth:1, margin:"2%", paddingLeft:"3%", backgroundColor:"white", borderColor:"grey", borderRadius:8}}
placeholder='password' onChangeText={props.handleChange('password')} value={props.values.password} />
<TouchableOpacity style={{backgroundColor:"#008b9a", justifyContent:"center", marginTop:"8%", alignItems:"center", width:"75%", height:40, borderRadius:16}}
onPress={props.handleSubmit}>
<Text style={{fontSize:16, fontWeight:"bold"}}>LOGIN</Text>
</TouchableOpacity>
</View>
)}
</Formik>
);
}
【问题讨论】:
标签: reactjs react-native keyboard-events dismiss