react-native - 为 textInput 中输入的文本添加背景颜色反应本机
问题描述
我想添加backgroundColor
到本机反应中输入的文本textInput
。
因此,当用户输入textInput
该背景颜色时,输入的文本会增加。
<TextInput
autoFocus
value={newTopic}
textAlignVertical="bottom"
onChangeText={val => dispatch(setNewTopic(val))}
style={{
paddingBottom: 0,
paddingTop: 2.5,
paddingRight: 38,
backgroundColor: "red",
}}
blurOnSubmit
placeholder={Strings.inputs.addTopicPlaceholder}
/>
添加backgroundColor: "red",
更改整个输入的背景颜色,但我只需要文本。
我怎样才能在本机反应中做到这一点?
解决方案
我找不到确切的解决方案。您可以尝试添加相对于 TextInput 的父视图的绝对视图,并根据输入的文本长度修改其宽度。
class Cat extends Component {
constructor(props) {
super(props);
this.state = {
sampleText: '',
};
}
onChangeText = (sampleText) => {
this.setState({
sampleText,
});
};
render() {
return (
<View style={{ flex: 1 }}>
<View style={{ position: 'relative' }}> // to keep the absolute view inside
<TextInput
style={{
backgroundColor: 'rgba(112,232,123,0.2)',
borderWidth: 1,
}}
onChangeText={this.onChangeText}
/>
<View
style={{
position: 'absolute',
backgroundColor: 'yellow',
zIndex: -1,
height: '100%',
width: this.state.sampleText.length * 7.5, //change based on fontSize
}}
/>
</View>
</View>
);
}
}
推荐阅读
- java - 如何修改包含在 JAR 中的文件,该文件是 IntelliJ 中的依赖项
- python - 从网站的不同选项卡中提取 html
- postgresql - 如何用匹配的值替换 JSONB 数组中的对象?
- java - 我可以从工作台连接到 AWS MySQL RDS 实例,但不能在我的 JDBC 应用程序中连接
- shell - 将 argparse 使用移动到单独的函数中
- python - 无法启动进程,工作目录 '\c\Users\hazzaldo\AppData\Local\Temp\tmpexejuk6bpycharm-management\setuptools-40.8.0' 不存在
- node.js - node js 不希望你完成一个函数的执行
- node.js - 如何为 React/Node 应用程序上的 POST 请求修复 net::ERR_EMPTY_RESPONSE
- express - 传入的 Express 参数与传入的不一样
- java - 为什么此活动显示在新的 Android 版本中而不是旧版本中