如何更改多个按钮中按下颜色的按钮 React Native [英] How to change button on pressed color in multiple buttons React Native
问题描述
我的 React Native 应用中有 3 个按钮.当用户单击按钮 1 时,我需要将其颜色更改为橙色.但其他按钮应具有默认颜色(灰色).如果用户下次单击按钮 3,颜色应更改为橙色,但第 1 个按钮颜色应重置为默认值.我对原生反应完全陌生,这就是我尝试过的.但它适用于所有按钮.我知道如果我可以拥有多个具有唯一 Id 的状态,就可以做到.但我不知道方法.
I have 3 buttons in my react native app. When user clicks button 1 I need to change it color to orange. But other buttons should have the default color (grey). If user clicks the button 3 next time, color should change to orange ,but that 1st button color should reset to default. I'm totally new to react native and this is what I tried. But it applies for all buttons. I know if I can have multiple states with unique Id , it can be done. But I don't know the method.
<Text style={ styles.switchButtonsTitle }>Choose Type of User</Text>
<TouchableOpacity onPress={(userType) =>
this.selectionOnPress("BASIC")} >
<Text style={_style}>
<Text style={styles.switchButtonsText}>BASIC</Text>
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={(userType) =>
this.selectionOnPress("INTERMEDIATE")}>
<Text style={_style}>
<Text style={styles.switchButtonsText}>INTERMEDIATE</Text>
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={(userType) =>
this.selectionOnPress("ADVANCED")}>
<Text style={{backgroundColor: this.state.backgroundColor}}>
<Text style={styles.switchButtonsText}>ADVANCED</Text>
</Text>
</TouchableOpacity>
selectionOnPress
selectionOnPress
selectionOnPress(userType) {
this.setState({
onClicked: true
});
}
道具
constructor(props) {
super(props);
this.state = {
onClicked: false
}
this.selectionOnPress = this.selectionOnPress.bind(this)
}
render(不加所有代码,只加本帖有用的代码)
render(not adding the all codes, only added the useful codes for this post)
render() {
var _style;
if (this.state.onClicked) { // clicked button style
_style = {
backgroundColor: "red"
}
}
else { // default button style
_style = {
backgroundColor: "blue"
}
}
推荐答案
我对你的代码做了一些修改
I make some modification on your code
export default class App extends Component {
constructor(props) {
super(props);
this.state = { selectedButton: null };
this.selectionOnPress = this.selectionOnPress.bind(this);
}
selectionOnPress(userType) {
this.setState({ selectedButton: userType });
}
render() {
return (
<View>
<Text style={styles.switchButtonsTitle}>
Choose Type of User
</Text>
<TouchableOpacity
onPress={() => this.selectionOnPress("BASIC")}
>
<Text
style={{
backgroundColor:
this.state.selectedButton === "BASIC"
? "red"
: "grey"
}}
>
<Text style={styles.switchButtonsText}>BASIC</Text>
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.selectionOnPress("INTERMEDIATE")}
>
<Text
style={{
backgroundColor:
this.state.selectedButton === "INTERMEDIATE"
? "red"
: "grey"
}}
>
<Text style={styles.switchButtonsText}>
INTERMEDIATE
</Text>
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.selectionOnPress("ADVANCED")}
>
<Text
style={{
backgroundColor:
this.state.selectedButton === "ADVANCED"
? "red"
: "grey"
}}
>
<Text style={styles.switchButtonsText}>
INTERMEDIATE
</Text>
</Text>
</TouchableOpacity>
</View>
);
}
}
... don't forget to define your styles
这篇关于如何更改多个按钮中按下颜色的按钮 React Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!