React native 反应本机:如何更改背景第二个操作
我认为问题很简单,但我似乎无法解决。我使用react native router flux库和NativeBase库作为按钮 这是我的代码:React native 反应本机:如何更改背景第二个操作,react-native,reactive-programming,react-native-ios,react-native-router-flux,react-android,React Native,Reactive Programming,React Native Ios,React Native Router Flux,React Android,我认为问题很简单,但我似乎无法解决。我使用react native router flux库和NativeBase库作为按钮 这是我的代码: <Button transparent onPress={Actions.MainOne } style={{ width: 50, height: 50 }} > <Text>Option1</Text> </Button> <Button transparent onPre
<Button transparent onPress={Actions.MainOne } style={{ width: 50, height: 50 }} >
<Text>Option1</Text>
</Button>
<Button transparent onPress={Actions.MainTwo} style={{ width: 50, height: 50 }} >
<Text>Option2</Text>
</Button>
选择1
选择2
我想改变按钮的背景颜色,每当我按下它,它是活跃的。如果我点击另一个按钮,我刚才按下的按钮会得到背景,第一个按钮会回到正常的透明背景。我真的不知道如何才能在按钮上添加两个操作。如果有人能帮忙,我将不胜感激。我不需要使用按钮库,所以欢迎对此有任何想法
谢谢大家! 我使用flux路由器在场景中导航。这是我按下按钮时更改按钮背景颜色的方式:
constructor() {
super();
state = {
color1: 'transparent',
color2: 'transparent',
}
}
setActive(button) {
if (button === 1) {
if (this.state.color1 === 'transparent') {
this.setState({
color1: 'red',
color2: 'transparent'
})
}
} else {
if (this.state.color2 === 'transparent') {
this.setState({
color2: 'red',
color1: 'transparent'
})
}
}
}
{ . . . }
<Button
onPress={this.setActive.bind(this, 1)}
style={{ width: 50, height: 50, backgroundColor: this.state.color1 }}
>
<Text>Option1</Text>
</Button>
<Button
this.setActive.bind(this, 2)
style={{ width: 50, height: 50, backgroundColor: this.state.color2 }}
>
<Text>Option2</Text>
</Button>
constructor(){
超级();
状态={
颜色1:'透明',
颜色2:'透明',
}
}
设置激活(按钮){
如果(按钮===1){
if(this.state.color1==='transparent'){
这个.setState({
颜色1:'红色',
颜色2:'透明'
})
}
}否则{
if(this.state.color2==='transparent'){
这个.setState({
颜色2:'红色',
颜色1:'透明'
})
}
}
}
{ . . . }
选择1
选择2
我使用flux路由器在场景中导航。这是我按下按钮时更改按钮背景颜色的方式:
constructor() {
super();
state = {
color1: 'transparent',
color2: 'transparent',
}
}
setActive(button) {
if (button === 1) {
if (this.state.color1 === 'transparent') {
this.setState({
color1: 'red',
color2: 'transparent'
})
}
} else {
if (this.state.color2 === 'transparent') {
this.setState({
color2: 'red',
color1: 'transparent'
})
}
}
}
{ . . . }
<Button
onPress={this.setActive.bind(this, 1)}
style={{ width: 50, height: 50, backgroundColor: this.state.color1 }}
>
<Text>Option1</Text>
</Button>
<Button
this.setActive.bind(this, 2)
style={{ width: 50, height: 50, backgroundColor: this.state.color2 }}
>
<Text>Option2</Text>
</Button>
constructor(){
超级();
状态={
颜色1:'透明',
颜色2:'透明',
}
}
设置激活(按钮){
如果(按钮===1){
if(this.state.color1==='transparent'){
这个.setState({
颜色1:'红色',
颜色2:'透明'
})
}
}否则{
if(this.state.color2==='transparent'){
这个.setState({
颜色2:'红色',
颜色1:'透明'
})
}
}
}
{ . . . }
选择1
选择2