android - 如果键盘打开,TouchableOpacity 的 onPress() 函数首先不起作用
问题描述
我使用 TouchableOpacity 作为我的提交按钮,在填写表单(登录/注册/...)后,当我单击提交按钮(TouchableOpacity)时,键盘隐藏(如果打开),然后我需要再次按下提交按钮然后调用 onPress 方法。
我想要实现的是,如果我单击提交按钮,无论键盘是否打开,都应该调用 onPress() 方法。我必须点击两次才能提交看起来不太好的表格。
此外,我还没有在 IOS、android 上测试过这种行为。
编辑:1
登录屏幕代码 =>
<KeyboardAvoidingView>
<ScrollView>
<View style={styles.container}>
<View style={styles.container_one}>
<Image
style={{ height: 100, marginTop: 40 }}
source={require("../images/logo.png")}
/>
</View>
<View style={styles.container_three}>
<View>
<View style={styles.container_two}>
<Text style={styles.text_style_two}>Login</Text>
</View>
<View>
<Text style={styles.text_style_three}>
Please enter mobile number
</Text>
<TextInput
style={styles.text_input_1}
autoCapitalize="none"
keyboardType="numeric"
onChangeText={text => {
this.setState({
username: text
});
}}
value={this.state.username}
/>
</View>
<View>
<Text style={styles.text_style_three}>
Please enter your password
</Text>
<TextInput
style={styles.text_input_1}
secureTextEntry={true}
autoCapitalize="none"
onChangeText={text => {
this.setState({
password: text
});
}}
value={this.state.password}
/>
</View>
<View
style={{
width: screenWidth / 1.3,
flexDirection: "row",
justifyContent: "center"
}}
>
<View>
<TouchableOpacity
style={styles.button_1}
onPress={() => {
if (this.state.username == "") {
alert("Username can not be blank");
} else if (this.state.password == "") {
alert("Password can not be blank");
} else {
this.submitLogin(
this.state.username,
this.state.password
);
}
}}
>
<Text style={CommonStyles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
</View>
</View>
</View>
<View style={{ alignItems: "center" }}>
<View style={{ marginBottom: 10 }}>
<Text style={{ fontSize: 20 }}>-OR-</Text>
</View>
<View style={{ marginBottom: 10 }}>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate("Registration");
}}
>
<Text style={styles.text_style_one}>Sign up here</Text>
</TouchableOpacity>
</View>
</View>
<View style={{ alignItems: "center" }}>
<View style={{ marginBottom: 10 }}></View>
<View style={{ marginBottom: 10 }}>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate("ForgotPassword");
}}
>
<Text style={styles.text_style_five}>forgot password?/Text>
</TouchableOpacity>
</View>
</View>
</View>
</ScrollView>
</KeyboardAvoidingView>
解决方案
只需添加keyboardShouldPersistTaps={'handled'}
到您的 ScrollView
<ScrollView keyboardShouldPersistTaps={'handled'}>
......
..............
</ScrollView>
推荐阅读
- javascript - javascript转换阿塞拜疆字母大“İ”(英文“I”)错了吗?
- c# - 单元测试控制器模拟 ISession
- assembly - MIPS 汇编器更改数组
- javascript - 带有 multipart/form-data 和 @ApiImplicitBody 的 Swagger 文档
- php - 单击时如何在ajax中刷新当前弹出窗口
- java - webflux spring boot 应用程序的过滤器,返回 ResponseEntity
- html - 最小宽度和宽度适合内容不适用于文本区域或输入
- c++ - 使用中断函数将 Keil5 C 项目转换为 C++ 项目错误
- angular - JEST:如何存根在类的构造函数中调用的方法
- sql - Postgresql 将两个表的 SUM 值与相同的列组合在一起