React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

一,需求与简单介绍

在开发项目时发现RN没有给提供RadioButton和RadioGroup这两个组件,只有CheckBox组件(不支持iOS),但是项目中确实有有一些地方需要使用到RadioButton和RadioGroup,比如默认地址的选择等。

需求:

  • 可以指定选中状态和未选中状态的颜色。
  • 可以指定该单选按钮是否可选:disabled。
  • 可以指定整体的样式,就像使用系统组件一样,这个用style来接收。
  • 可以自定义宽(width)高(height)。

二,RadioButton(如需完整的代码,请留言评论)

在RN中控制一个View动态改变需要使用到state,这里定义一个state变量selected来记录RadioButton是否被选中,并且可以默认选中某一个,为true为选中状态,false为未选中状态:(如需完整的代码,请留言评论)

 this.setState({
selectedIndex: nextProps.selectedIndex//从RadioGroup组件传入
})

state变量和动态控制一个组件的改变,但是组件改变之前仍然可能会显示一些东西,这些东西用props来控制,而且可以用这些props定义一些默认的属性,例如我们可以定义默认的颜色等:

 RadioGroup.defaultProps = {
size: defaultSize,
thickness: defaultThickness,
color: defaultColor,
highlightColor: null,
}

在使用时我们可能会给这个RadioButton添加style属性,例如单选按钮的宽,高颜色等,以及选中的小圆点颜色,宽,高等等等,这个是在外面设置的,在内部我们同样会设置style属性

  getRadioStyle(){
return {
height: this.context.size,
width: this.context.size,
borderRadius: this.context.size / 2,
borderWidth: this.context.thickness,
borderColor: this.props.isSelected && this.props.activeColor?this.props.activeColor:this.context.color,
}
} getRadioDotStyle(){
return {
height: this.context.size / 2,
width: this.context.size / 2,
borderRadius: this.context.size / 4,
backgroundColor: this.props.color || this.props.activeColor,
}
}

给最外层的View添加TouchableWithoutFeedback组件,添加点击事件以及是否可点击状态:

  <View style={{opacity: this.props.disabled?0.4:1}}>
<TouchableWithoutFeedback
disabled={this.props.disabled}//是否可点击
onPress={() => this.context.onSelect(this.props.index, this.props.value)}//选中事件
>
{children}
</TouchableWithoutFeedback>
</View>

选中之后的样式选择:(如需完整的代码,请留言评论)

  isSelected(){
if(this.props.isSelected)
return <View style={this.getRadioDotStyle()}/>
}

三,RadioGroup(如需完整的代码,请留言评论)

使用RadioButton大部分情况是多个共同使用,而且只能有一个被选中,android中就有这样的组件,但是在RN中没有找到,其实这个也很容易实现,原理是通过RadioGroup来生成多个RadioButton并且持有这些RadioButton的引用,当一个被选中的时候把其他的置为不选中(如需完整的代码,请留言评论)。

 if(nextProps.selectedIndex != this.prevSelected){
this.prevSelected = nextProps.selectedIndex
this.setState({
selectedIndex: nextProps.selectedIndex
})
}

使用RadioGroup时给这个RadioButton传递多个即可,然后RadioGroup通过数组来创建RadioGroup,因为同样要指定RadioButton的样式,所以在外部使用时直接把style的各种样式和属性一并传递给RadioGroup,RadioGroup在创建RadioButton时把这些样式属性再传递给RadioButton(如需完整的代码,请留言评论):

 <View style={this.props.style}>
{radioButtons}
</View>
RadioGroup.childContextTypes = {
onSelect: PropTypes.func.isRequired,
size: PropTypes.number.isRequired,
thickness: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
activeColor: PropTypes.string,
highlightColor: PropTypes.string,
}

获取选中事件的函数(如需完整的代码,请留言评论):

 onSelect(index, value){
this.setState({
selectedIndex: index
})
if(this.props.onSelect)
this.props.onSelect(index, value)
}

四,使用实例(如需完整的代码,请留言评论)

已实现的样列(如需完整的代码,请留言评论):

组件代码实现:

   <RadioGroup
style={{ backgroundColor: '#fff' }}
onSelect={(index, value) => this.onSelect(index, value)}
selectedIndex={this.state.selectedIndex}
>
{UsersAddress.map((model, i) => { return (
<RadioButton
key={i}
value={model}
selectedIndex={1}
style={{ backgroundColor: '#fff', marginBottom: 12, borderBottomColor: '#e4e4e4', borderBottomWidth: 12 }}>
<Text>张三</Text>
</RadioButton>
<RadioButton
key={i}
value={model}
selectedIndex={1}
style={{ backgroundColor: '#fff', marginBottom: 12, borderBottomColor: '#e4e4e4', borderBottomWidth: 12 }}>
<Text>李四</Text>
</RadioButton>
<RadioButton
key={i}
value={model}
selectedIndex={1}
style={{ backgroundColor: '#fff', marginBottom: 12, borderBottomColor: '#e4e4e4', borderBottomWidth: 12 }}>
<Text>王二</Text>
</RadioButton>
</RadioGroup>
  onSelect(index, value) {
//alert(JSON.stringify(value))
//this.openAduice()
this.setState({
text: `Selected index: ${index} , value: ${value}`,
addressId: value.id
})
}

(如需完整的代码,请留言评论,留下联系方式)

React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)的更多相关文章

  1. React Native 组建之IOS和Android通用抽屉

    /** * Sample React Native App * https://github.com/facebook/react-native * @flow *npm:https://www.np ...

  2. 封装 React Native 原生组件(iOS / Android)

    封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...

  3. 如何用 React Native 创建一个iOS APP?

    诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...

  4. 如何用 React Native 创建一个iOS APP?(二)

    我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...

  5. React Native之通知栏消息提示(android)

    React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...

  6. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  7. React native 之设置IOS的图标,名称和启动图(下篇文章会讲到RN的android的相关设置)

    1.首先,app的名称: 如图所示:我的工程名叫BOOk 在BOOk下面的info.plist的文件里设置app的相关信息:比如Bundle name就是设置APP的名称 2.App的图标:(这里注意 ...

  8. React Native热更新(iOS)-Pushy

    React Native的出现,使的开发iOS代码出现了更便捷的方式.由于RN是使用脚本语言编写的,实现了"解释执行"的方式,而这种执行方式的修改只需替换脚步即可,不需要重新发布程 ...

  9. React Native项目集成iOS原生模块

    今天学习一下怎么在React Native项目中集成iOS原生模块,道理和在iOS原生项目中集成React Native模块类似.他们的界面跳转靠的都是iOS原生的UINavigationContro ...

随机推荐

  1. 一张有料的图片!!!附文件-图片合成器C语言实现算法

    凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.打开F盘,先找两个文件,一个是压缩文件(2.rar),另一个是一张图片(1.jpg),将其放到F盘目录下, ...

  2. Django-rest-framework 接口实现 权限:(Permission)

    权限:(Permission) rest_framework 中 已经定义好了 权限 在 from rest_framework.permissions 中查看所有的权限 from rest_fram ...

  3. Python学习之装饰器进阶

    函数知识回顾: 函数的参数分为:实参和形参. 实参:调用函数的时候传入的参数: 形参:分为3种(位置参数.默认参数.动态传参) 位置参数:必须传值 def aaa(a,b): print(a,b) a ...

  4. executequery要求已打开且可用的connection,连接的当前状态为已关闭

    问题: executequery要求已打开且可用的connection,连接的当前状态为已关闭 错误原因: 连接的当前状态为已关闭.或者只创建了Connection对象,没有调用Connection. ...

  5. 001_python单元测试

    一.同事推荐的 pytest库 ==> 官网:http://doc.pytest.org/en/latest/ github地址==>https://github.com/pytest-d ...

  6. 强大的原生DOM选择器querySelector和querySelectorAll

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

  7. autoware

    在 Autoware目录下执行 git checkout 将版本切换到1.10.0 因为最新版驱动有问题然后执行 sudo apt-get update sudo apt-get install -y ...

  8. open-falcon之使用mail-provider发邮件(支持smtp SSL协议)

    一.首先确定go语言安装环境配置好 1.进入官网下载源码包 https://golang.org/dl/ 2.解压缩,配置环境变量 在/etc/profile最后加上export PATH=$PATH ...

  9. Android hook神器frida(二)

    1.关于使用frida遇到的一些问题 (1)如果出现以下错误: 可以通过以下方式关闭SELinux,在adb shell中执行: > /sys/fs/selinux/enforce 或者 set ...

  10. redis底层设计(一)——内部数据结构

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...