javascript - 在 React Native 中提交表单时处理键盘事件?
问题描述
我创建了一个登录表单,每当我点击输入框时,键盘就会打开,但是当我按下提交按钮时,键盘会先关闭,然后我再次按下提交按钮,然后表单就会提交。但我希望每当我点击提交按钮时,键盘会自动关闭,并且表单会同时提交。
import React, { Component } from "react";
import { View, Text, Button,KeyboardAvoidingView } from "react-native";
import { Button, Input } from "native-base";
export default class TestForm extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
};
}
handleSignupChange = (data, text) => {
this.setState({ [data]: text });
};
submitForm = () => {
const {email,password}=this.state;
console.log('form value',email,password);
};
render() {
return (
<KeyboardAvoidingView enabled={true}>
<Text> TestForm </Text>
<Input
onChangeText={(e) => this.handleSignupChange("email", e)}
placeholder="email"
/>
<Input
onChangeText={(e) => this.handleSignupChange("password", e)}
placeholder="password"
/>
<Button onPress={this.submitForm} />
</KeyboardAvoidingView>
);
}
}
解决方案
尝试这个:
您导入Keyboard
fromreact-native
并在函数开头调用它以隐藏键盘
import { Keyboard } from 'react-native';
submitForm = () => {
Keyboard.dismiss
const {email,password}=this.state;
console.log('form value',email,password);
};
推荐阅读
- database - 无法在 REST 中将字符串转换为时间戳
- excel - 如何根据前 5 个小计值有条件地格式化标题单元格?
- angularjs - @Input 从 AngularJS 获取变量名
- python - 如何检查数据框列是否包含数值?蟒蛇 3.6
- nginx - 无法连接到 socket.io 服务器
- java - java - 如何在java 8中将catch最终重新组合成一种方法?
- php - Laravel 验证规则 - 必须至少更改一个输入值
- java - 附加新的 OneToMany 实体时,瞬态字段丢失
- excel - 任何可能的VBA代码在excel中加倍并按顺序编号名称?
- css - 作用于中点的 CSS 动画计时功能