gpt4 book ai didi

android - 在 React Native 中取消焦点文本输入

转载 作者:IT王子 更新时间:2023-10-28 23:34:09 25 4
gpt4 key购买 nike

我正在使用 React Native 构建一个 Android 应用。

如何强制 TextInput 为“unFocus”,这意味着光标在文本字段内闪烁。 isFocused()onFocus() 有函数,但我实际上如何让文本字段放弃焦点。您会认为一旦我按 Enter 键,它就会自动执行此操作,但事实并非如此。

   import React, {Component} from 'react';
import { AppRegistry, Text, View, StyleSheet, TextInput, TouchableOpacity}
from 'react-native';

var SHA256 = require("crypto-js/sha256");

export default class LoginForm extends Component{


constructor(props){
super(props);
this.state = {
email: '',
password:''
};
}

tryLogin = () => {
if(this.state.email=="email123" && this.state.password == "password"){
console.log("password verified");
this.props.navigator.replace({
title: 'Dashboard'
});
}

console.log(this.state.email);
console.log(this.state.password);
console.log("Hash" + SHA256(this.state.password));
}

render(){
return(
<View style={styles.container}>
<TextInput
style={styles.input}

placeholder="Email address"
placeholderTextColor="white"
onChangeText={(email) => this.setState({email})}>
</TextInput>
<TextInput style={styles.input}
placeholder="Password"
placeholderTextColor="white"
secureTextEntry
onChangeText={(password) => this.setState({password})}>
</TextInput>

<TouchableOpacity style={styles.loginButtonContainer} onPress={this.tryLogin}>
<Text style={styles.loginButtonText}>LOGIN</Text>
</TouchableOpacity>
</View>
);
}
}

AppRegistry.registerComponent('LoginForm', () => LoginForm);

const styles = StyleSheet.create({
container: {
padding: 20
},
input:{
height: 40,
backgroundColor: '#e74c3c',
marginBottom: 20,
color: 'white',
paddingHorizontal: 15,
opacity: .9
},
loginButtonContainer:{
justifyContent: 'center',
backgroundColor: '#bc4c3c',
paddingVertical:15

},
loginButtonText:{
textAlign:'center',
color:'white',
fontWeight: '700',
fontSize: 24

}

})

这对于真实用户来说可能并不重要,但我只是在模拟,如果我想重新加载它会很烦人。

最佳答案

更好的方法是使用 ScrollViewKeyboard.dismiss。当用户在 textInput 之外点击时使用 ScrollView,键盘会关闭。这样做是因为 ScrollView 的默认属性 keyboardShouldPersistTapsnever。这是用户期望的行为。为了关闭键盘,或者等效地模糊 textInput,当用户点击登录按钮时,将 Keyboard.dismissed() 添加到 tryLogin功能。

import React, {Component} from 'react';
import { AppRegistry, Text, View, StyleSheet, TextInput, TouchableOpacity, ScrollView, Keyboard}
from 'react-native';
var SHA256 = require("crypto-js/sha256");

export default class LoginForm extends Component{


constructor(props){
super(props);
this.state = {
email: '',
password:''
};
}

tryLogin = () => {
Keyboard.dismiss();
if(this.state.email=="email123" && this.state.password == "password"){
console.log("password verified");
this.props.navigator.replace({
title: 'Dashboard'
});
}

console.log(this.state.email);
console.log(this.state.password);
console.log("Hash" + SHA256(this.state.password));
}

render(){
return(
<ScrollView style={styles.container}>
<TextInput
style={styles.input}

placeholder="Email address"
placeholderTextColor="white"
onChangeText={(email) => this.setState({email})}>
</TextInput>
<TextInput style={styles.input}
placeholder="Password"
placeholderTextColor="white"
secureTextEntry
onChangeText={(password) => this.setState({password})}>
</TextInput>

<TouchableOpacity style={styles.loginButtonContainer} onPress={this.tryLogin}>
<Text style={styles.loginButtonText}>LOGIN</Text>
</TouchableOpacity>
</ScrollView>
);
}
}

AppRegistry.registerComponent('LoginForm', () => LoginForm);

const styles = StyleSheet.create({
container: {
padding: 20
},
input:{
height: 40,
backgroundColor: '#e74c3c',
marginBottom: 20,
color: 'white',
paddingHorizontal: 15,
opacity: .9
},
loginButtonContainer:{
justifyContent: 'center',
backgroundColor: '#bc4c3c',
paddingVertical:15

},
loginButtonText:{
textAlign:'center',
color:'white',
fontWeight: '700',
fontSize: 24

}

})

关于android - 在 React Native 中取消焦点文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com