【发布时间】:2018-07-17 06:25:19
【问题描述】:
我在一个活动中有 5 个开关都可以正常工作。但问题是所有这些都是相互关联的,即如果打开一个开关,所有开关都会打开。
如何让它们独立开关而不影响其他开关动作?
我的代码 -
state = {
colorTrueSwitchIsOn: true,
colorFalseSwitchIsOn: false,
};
class Notification extends Component {
static navigationOptions = ({
navigation
}) => ({
header: props => < Header
navigation = {
navigation
}
title = {
'NOTIFICATION SETTINGS'
}
toggleDrawer /
>
})
constructor(props) {
super(props);
this.state = {
SwitchOnValueHolder: false
};
}
ShowAlert = (value) => {
this.setState({
SwitchOnValueHolder: value
})
if (value == true) {
//Perform any task here which you want to execute on Switch ON event.
Toast.show('Switch is On');
}
else {
//Perform any task here which you want to execute on Switch OFF event.
Toast.show('Switch is On');
}
}
render() {
return ( <
View style = {
styles.container
} >
<
ScrollView >
<
View style = {
{
flex: 1,
flexDirection: 'row'
}
} >
<
View style = {
{
width: '25%',
height: 50,
}
} >
<
Switch onValueChange = {
(value) => this.setState({
colorTrueSwitchIsOn: value
})
}
onTintColor = "#FFE2C6"
thumbTintColor = "#FF952E"
tintColor = "#D7D0C9"
value = {
this.state.colorTrueSwitchIsOn
}
/>
<
/View>
<
/View>
<
View style = {
{
flex: 1,
flexDirection: 'row'
}
} >
<
View style = {
{
width: '25%',
height: 50,
}
} >
<
Switch onValueChange = {
(value) => this.setState({
colorTrueSwitchIsOn: value
})
}
onTintColor = "#FFE2C6"
thumbTintColor = "#FF952E"
tintColor = "#D7D0C9"
value = {
this.state.colorTrueSwitchIsOn
}
/>
<
/View>
<
/View>
<
View style = {
{
flex: 1,
flexDirection: 'row',
marginBottom: 20
}
} >
<
View style = {
{
width: '25%',
height: 50,
top: 10
}
} >
<
Switch onValueChange = {
(value) => this.setState({
colorTrueSwitchIsOn: value
})
}
onTintColor = "#FFE2C6"
thumbTintColor = "#FF952E"
tintColor = "#D7D0C9"
value = {
this.state.colorTrueSwitchIsOn
}
/>
<
/View>
<
/View>
<
View style = {
{
flex: 1,
flexDirection: 'row'
}
} >
<
View style = {
{
width: '25%',
height: 50,
}
} >
<
Switch onValueChange = {
(value) => this.setState({
colorTrueSwitchIsOn: value
})
}
onTintColor = "#FFE2C6"
thumbTintColor = "#FF952E"
tintColor = "#D7D0C9"
value = {
this.state.colorTrueSwitchIsOn
}
/>
<
/View>
<
/View>
<
View style = {
{
flex: 1,
flexDirection: 'row'
}
} >
<
View style = {
{
width: '25%',
height: 50,
}
} >
<
Switch onValueChange = {
(value) => this.setState({
colorTrueSwitchIsOn: value
})
}
onTintColor = "#FFE2C6"
thumbTintColor = "#FF952E"
tintColor = "#D7D0C9"
value = {
this.state.colorTrueSwitchIsOn
}
/>
<
/View>
<
/View>
<
/View>
<
/ScrollView>
<
/View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignContent: 'center',
backgroundColor: "#fff",
},
layout: {
marginTop: 15,
marginBottom: 5,
marginLeft: 15,
marginRight: 10,
}
})
export default Notification
我该如何解决这个错误? 我尝试将其调用为不起作用的不同开关!
【问题讨论】:
-
This 链接可能对您有用。
标签: android react-native