【发布时间】:2018-06-23 23:09:25
【问题描述】:
我目前正在尝试使用道具 onFocus 更改单个 Textinput 元素的边框颜色。我正在使用一个包含两组样式的数组,第一个“styles.textInput”应该是第一个加载的样式。第二个应该只在切换为真时加载,然后它应该加载第二个样式,即“styles.textInputAlt”
现在,BOTH textInputs 的borderColor 正在改变。如何确保唯一获得更改的 textInput 是当前 onFocus 的?
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
TouchableHighlight,
View,
} from 'react-native';
export default class Highlight extends Component{
constructor(){
super()
this.state = {
toggle: false,
}
}
hasFocus(){
this.setState({
toggle: !this.state.toggle
})
}
lostFocus(){
this.setState({
toggle:this.state.toggle,
})
}
render(){
return(
<View style={styles.container}>
<TextInput
style={[styles.textInput, this.state.toggle && styles.textInputAlt]}
onFocus={()=>this.hasFocus()}
onBlur={()=>this.lostFocus()}
/>
<TextInput
style={[styles.textInput, this.state.toggle && styles.textInputAlt]}
onFocus={()=>this.hasFocus()}
onBlur={()=>this.lostFocus()}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
textInput: {
borderColor: '#000',
borderWidth: 2.0,
backgroundColor: '#fff',
height: 40,
marginLeft: 60,
marginRight: 60,
marginBottom: 30,
padding: 2,
},
textInputAlt: {
borderColor: '#e71636',
},
button: {
height: 50,
backgroundColor: '#48bbec',
alignSelf: 'stretch',
marginTop: 10,
marginLeft: 60,
marginRight: 60,
justifyContent: 'center'
},
buttonText: {
fontSize: 22,
color: '#FFF',
alignSelf: 'center'
}
});
【问题讨论】:
标签: css react-native react-native-android textinput