移动跨平台ReactNative开关组件Switch【15】

在这里插入图片描述
前端江太公


React Native,是一个混合移动应用开发框架,是目前流行的跨平台移动应用开发框架之一。React Native 采用不同的方法进行混合移动应用开发。它不会生成原生 UI 组件,而是基于 React,React Native
是一个用于构建基于 Web 的交互界面的 JavaScript 库,因此会有更丰富的 UI 体验效果,同时也能够很好地调用底层框架的UI使用。

React Native系列导航
01-React Native 基础教程
02-安装ReactNative
03-ReactNative目录结构
04-ReactNative视图View
05-ReactNative组件样式style
06-ReactNative文本组件Text
07-ReactNative组件状态state
08-ReactNative组件属性props
09-ReactNative输入组件TextInput
10-ReactNative图片组件Image
11-ReactNative活动指示器组件
12-ReactNative弹出框Alert
13-ReactNative存储数据组件AsyncStorage
14-ReactNative动画组件Animated
15-ReactNative开关组件Switch
16-状态栏组件StatusBar
17-ReactNative滚动视图ScrollView
18-ReactNative选择器Picker
19-ReactNative网络请求

React Native 开关组件 Switch

如果要在两个值之间切换,或者要在两个状态之间切换,我们可以使用 React Native 提供的 开关组件 Switch

开关组件,顾名思义,就像我们日常电灯的开关一样:按一下开,再按一下关,再按一下又开

开关组件 Switch 在 Android 端的样式如下

React Native 开关组件 Switch on

React Native 开关组件 Switch

引入组件

import { Switch } from 'react-native'

使用语法

<Switch
   onValueChange = {function(value){}}
   thumbColor    = {color}
   trackColor    = {{false:color,true:color}}
   onChange      = {function(event){}}
   value         = {true|false}/>

Switch 只有两个值 truefalse,都是布尔类型。

  • true 表示开关的 状态。
  • false 表示开关的 状态,默认值。

这两个值是固定的,我们不能变更。

如果我们要改变开关的初始状态,可以使用 value 属性来设置初始值,不过只能设置为 truefalse

注意:value 是必填属性,如果不设置,开关的状态看起来用于处于 状态。

Switch 还有两个事件回调函数 onValueChangeonChange。前者当开关的值发生改变时触发,参数是 开关变更后的新值。 后者当用户尝试改变开关状态时触发,参数是 事件

开关的外观基本是固定的,我们不能改变,唯一能做的就是改变颜色。这里有三个颜色可以改变,一个是导轨的颜色,分为 状态下导轨的颜色和 状态下导轨的颜色。还有一个是 滑块 的颜色。

因此,如果你要设置导轨的颜色,需要传递一个对象,格式如下

{false:color,true:color}

例如

{false:'#eeeeee',true:'#333333'}

当开关处于开状态下时的导轨颜色为 #333333,处于关状态下时的颜色为 #eeeeee

范例 1 : 最基本的使用

React Native Switch 最基本的使用,仅仅作为状态展示组件,那么只需要一个属性即可,那就是 value 用于设置开关的初始值。

App.js
import React, { Component } from 'react'
import { View, Text, Switch, StyleSheet } from 'react-native'

export default class App  extends Component {
    constructor() {
        super();
        this.label = {false:'关',true:'开'}
        this.state = {
            switch1Value: true,
        }
    }

    render() {
        return (
            <View style = {styles.container}>
                <Switch  
                    value= {this.state.switch1Value} 
                />
                <View>
                    <Text>Switch 当前的状态是:{this.label[this.state.switch1Value]}</Text>
                </View>
            </View>
        )
    }
}

const styles = StyleSheet.create ({
    container: {
        flex: 1,
        alignItems: 'center',
        marginTop: 100
   }
})

运行效果如下,这时候无论怎么切换状态,都是处于 的状态。

范例 1 : 可响应状态变更

Switch 组件如果要响应我们的触摸操作,就需要使用 onValueChange 来设置 value 的值。

App.js
import React, { Component } from 'react'
import { View, Text, Switch, StyleSheet } from 'react-native'

export default class App  extends Component {
    constructor() {
        super();
        this.label = {false:'关',true:'开'}
        this.state = {
            switch1Value: true,
        }
    }

    toggleSwitch = (value) => {
        this.setState({switch1Value: value})
    }

    render() {
        return (
            <View style = {styles.container}>
                <Switch 
                    onValueChange = {this.toggleSwitch} 
                    value= {this.state.switch1Value} 
                />
                <View><Text>Switch 当前的状态是:{this.label[this.state.switch1Value]}</Text></View>
            </View>
        )
    }
}

const styles = StyleSheet.create ({
    container: {
        flex: 1,
        alignItems: 'center',
        marginTop: 100
   }
})

演示效果如下

范例3 :定制外观

如果我们还需要对外观的颜色加以定制,可以设置 thumbColortrackColor

比如说我们要将 Switch 的外观定制为下面的样子。

React Native 开关组件 Switch on

可以设置属性

<Switch
   thumbColor    = {"#000000"}
   trackColor    = {{false:"#eeeeee",true:"#999999"}}
/>
App.js
import React, { Component } from 'react'
import { View, Text, Switch, StyleSheet } from 'react-native'

export default class App  extends Component {
    constructor() {
        super();
        this.label = {false:'关',true:'开'}
        this.state = {
            switch1Value: true,
        }
    }

    toggleSwitch = (value) => {
        this.setState({switch1Value: value})
    }

    render() {
        return (
            <View style = {styles.container}>
                <Switch 
                    thumbColor={"#000000"}
                    trackColor={{false:"#eeeeee",true:"#999999"}}
                    onValueChange = {this.toggleSwitch} 
                    value= {this.state.switch1Value} 
                />
                <View><Text>Switch 当前的状态是:{this.label[this.state.switch1Value]}</Text></View>
            </View>
        )
    }
}

const styles = StyleSheet.create ({
    container: {
        flex: 1,
        alignItems: 'center',
        marginTop: 100
   }
})

h1Value]}

)
}
}

const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: ‘center’,
marginTop: 100
}
})


演示
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江一铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值