reactjs - 在 TextInput React Native 中输入文本时不应删除占位符文本
问题描述
我需要一些帮助来使用 TextInput react native。
我想要实现的是,当用户开始在表单字段中输入时,我需要在 TextInput 中附加“月份”文本。如果您查看以下 Tenure 和 Paid EMI 的屏幕截图,则值为 123 和 12。我需要在用户输入值时动态添加“123 个月”和“12 个月”月份。
例如,如果用户输入 2,则应显示为“2 个月”,如果用户删除 2,则默认占位符文本应显示为“36 个月”。
更重要的是“月份”应该是不可编辑的,用户应该只能输入或更新数值。例如:如果 TextInput 中的值为“12 个月”,并且用户希望将其更新为“32 个月”。然后他/她单击 TextInput 字段并单击退格键 2 次,然后输入新值“32”。在用户界面中,它将显示为“32 个月”。
[注意:- 只有我需要捕获数值,我不希望捕获月份值。它只需要在 TextInput 中为最终用户显示。“32 个月”将显示在表单字段中,但状态值应仅为“32”。
[EDITED-1 添加代码] 渲染功能:-
<View style={{marginTop:height(2),width:width(90)}}>
<View style={{height:height(3),marginLeft:width(1)}}>
<Text style={{fontSize:16,fontFamily:"Ubuntu-Bold"}}>Tenure</Text>
</View>
<View style={{marginTop: height(1),width:width(90),alignItems:"center"}}>
<TouchableOpacity style={{width:width(90),flexDirection:"row",backgroundColor:"#F6F6F6",alignItems:"center",justifyContent:"center",height:height(6),borderRadius:12}}>
<TextInput placeholder="36 months"
textAlign={"center"}
value={this.state.tenure}
maxLength={3}
onChangeText={this.onTenureChanged.bind(this)}
keyboardType="number-pad"/>
</TouchableOpacity>
</View>
</View>
<View style={{marginTop:height(4.5),width:width(90),bottom: height(3)}}>
<View style={{height:height(3),marginLeft:width(1)}}>
<Text style={{fontSize:16,fontFamily:"Ubuntu-Bold"}}>Paid EMI</Text>
</View>
<View style={{marginTop: height(1),width:width(90),alignItems:"center"}}>
<TouchableOpacity style={{width:width(90),flexDirection:"row",backgroundColor:"#F6F6F6",alignItems:"center",justifyContent:"center",height:height(6),borderRadius:12}}>
<TextInput placeholder="15 months"
textAlign={"center"}
maxLength={3}
value={this.state.paidemi}
onChangeText={this.onPaidEMIChanged.bind(this)}
keyboardType="number-pad"/>
</TouchableOpacity>
</View>
</View>
Javascript函数:-
onTenureChanged(itemValue){
this.setState({tenure: itemValue})
}
onPaidEMIChanged(itemValue){
this.setState({paidemi: itemValue})
}
解决方案
推荐阅读
- react-native - [未处理的承诺拒绝:TypeError:未定义不是对象(评估'clarifai.GENERAL.MODEL')]
- matlab - matlab 编码器要求比普通 matlab 更严格
- python - PowerBI 没有将数据字段传递给 Python 脚本
- java - 在 apache flink 的 protobuf 事件中反序列化事件
- python - torch.mean() 的源代码位置
- python-3.x - 将胶水 pypspark 错误写入文本文件
- c++ - 如何使用 Canny 转 32bit 灰度?
- sql-server - 为什么还原数据库后不存储表和存储过程?
- vue.js - 无法通过@click 调用 vs- 下拉列表中的函数
- mysql - mysql查询从3个类别中获取随机项目