react native Switch使用详解
2017-04-18 10:11
671 查看
Switch:开关控件
主要属性:
value:是否打开。默认为false
disabled:是否禁用 默认false
onTintColor:打开时背景色
thumbTintColor:圆形按钮的背景颜色
tintColor:关闭时的边框颜色(iOS)或者背景颜色(Android)。
onValueChange:value值发生变化时的回调,参数为当前Switch的值。
提示:如果想要切换Switch,必须改变value值,否则Switch保持不变。
Demo:
效果图
github下载地址
主要属性:
value:是否打开。默认为false
disabled:是否禁用 默认false
onTintColor:打开时背景色
thumbTintColor:圆形按钮的背景颜色
tintColor:关闭时的边框颜色(iOS)或者背景颜色(Android)。
onValueChange:value值发生变化时的回调,参数为当前Switch的值。
提示:如果想要切换Switch,必须改变value值,否则Switch保持不变。
Demo:
import React, {Component} from 'react'; import { StyleSheet, View, Switch, Text, Button, } from 'react-native'; export default class SwitchDemo extends Component { state = { value: false, disabled: false, changeTxt:'切换Switch', } render() { return ( <View style={{flex:1}}> <View style={{flexDirection:'row',paddingLeft:20}}> <Button title={this.state.value?'关闭':'打开'} onPress={()=>{this.setState({value:!this.state.value});}}></Button> <Switch style={{marginLeft:20}} value={this.state.value} onTintColor='red' tintColor='blue' thumbTintColor='black'/> </View> <View style={{flexDirection:'row',paddingLeft:20,marginTop:20}}> <Button title={this.state.disabled?'启用':'禁用'} onPress={()=>{this.setState({disabled:!this.state.disabled});}}></Button> <Switch style={{marginLeft:20}} value={true} disabled={this.state.disabled}/> </View> <View style={{flexDirection:'row',paddingLeft:20,marginTop:20}}> <Text>{this.state.changeTxt}</Text> <Switch value={this.state.value} onValueChange={(value)=>{ this.setState({ value:value, changeTxt:value?'switch 打开了':'switch 关闭了' }); }}/> </View> </View> ); } }
效果图
github下载地址
相关文章推荐
- React Native之Redux使用详解之Reducers(30)
- react-native-router-flux 使用详解(一)
- react native DatePickerIOS 使用详解
- react native ViewPagerAndroid 使用详解
- react native ProgressBarAndroid 使用详解
- React Native按钮详解|Touchable系列组件使用详解
- react native ScrollView 使用详解
- react native DrawerLayoutAndroid 使用详解
- react native SectionList 使用详解
- react native TabBarIOS 使用详解
- react-native-router-flux 使用详解(一)
- 【React Native开发】React Native控件之WebView组件详解以及实例使用(22)
- react-native-router-flux 使用详解(三)
- react native Modal 使用详解
- react native TextInput 使用详解
- RN开源UI组件之react-native-button 使用详解
- react native SegmentedControlIOS 使用详解
- react native Button 使用详解
- react native RefreshControl 使用详解
- react native Picker 使用详解