javascript - React-Native 密码 textInput 的问题
问题描述
我创建了一个 textInput 组件供用户输入密码。
当用户输入密码时,密码显示为隐藏。
但是,当用户按下“显示密码”按钮时,我希望更改密码颜色以使其可见。
我有这个功能,但是当它执行时,它不会更改颜色,而是删除密码文本。
功能:
if (...) {
....
this.state.passTextColor = 'black';
....
}
任何帮助将非常感激!
解决方案
我根据您的要求创建了一个示例应用程序。
import React, { Component } from "react";
import { View, TextInput, Button, StyleSheet } from "react-native";
export default class Example extends Component {
state = {
password: "",
isPasswordHide: true
};
onChangeText = text => {
this.setState({
password: text
});
};
changePasswordVisibility = () => {
this.setState({
isPasswordHide: !this.state.isPasswordHide
});
};
render() {
return (
<View style={styles.container}>
<TextInput
style={
this.state.isPasswordHide
? styles.textInput
: [styles.textInput, { color: "#ED3C20" }]
}
onChangeText={this.onChangeText}
placeholder="Enter Your Password"
value={this.state.password}
secureTextEntry={this.state.isPasswordHide}
/>
<Button
title={this.state.isPasswordHide ? "Show Password" : "Hide Password"}
onPress={this.changePasswordVisibility}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
textInput: {
width: "80%",
height: 40,
borderColor: "gray",
borderWidth: 1,
padding: 5,
marginBottom: 10
}
});
希望这对您有所帮助。随意怀疑。
推荐阅读
- javascript - 转到新页面时运行脚本
- arrays - 尽管定义了数组大小,但 C 中的预期常量表达式
- mongodb - 选择一个数组的一个数组匹配一个条件的文档
- node.js - Node.JS AES 解密截断初始结果
- reactjs - 使用 getServerSideProps 保护路由
- javascript - 在 jsfuck 中获取“C”字母的替代方法
- python - 如何通过内联电报命令发送随机字符串?
- java - spring batch MultiResourceItemWriter:如何唯一标识每个写入的文件
- linux - 关于为来电分配完整电话号码给 voip 服务器的指南
- r - 将多个变量重新编码为“其他”