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. CSS染色图标(图片)

    之前一直以为用background引入的图标无法染色(非字体图标),现在才知道有黑科技可以用,就是利用drop-shadow. 代码示例 <!DOCTYPE html> <html& ...

  2. ubuntu使用遇到的问题

    1.不适当操作,改了sudoers的权限 scdev@scdev1005:~$ sudo vim /etc/profilesudo: /etc/sudoers is owned by uid 1000 ...

  3. C#反射の一个泛型反射实现的网络请求框架

    点击下载源码 C#反射の反射详解(点击跳转)C#反射の反射接口(点击跳转)C#反射反射泛型接口(点击跳转)C#反射の一个泛型反射实现的网络请求框架(点击跳转)

  4. CentOS 7 上安装vim(默认未安装)

    今天使用CentOS 7,发现未安装vim,所以重新安装 执行命令: yum -y install vim* 然后就可以使用了

  5. Python 中两个字典(dict)合并

    dict1 = { "name":"owen", "age": 18 } dict2 = { "birthday": & ...

  6. vue 数据绑定实现的核心 Object.defineProperty()

    vue深入响应式原理 现在是时候深入一下了!Vue 最独特的特性之一,是其非侵入性的响应式系统.数据模型仅仅是普通的 JavaScript 对象.而当你修改它们时,视图会进行更新.这使得状态管理非常简 ...

  7. Unicode与UTF-8关系

    Unicode字符集合 Unicode 也称为 UCS(Universal Coded Character Set:国际编码字符集合) 是一个字符集合. 对世界上大部分的文字系统进行了整理,编码,使电 ...

  8. Python打包—Pyinstaller

    2018-09-27 21:12:05   一 前言 在windows平台学习python的过程中,你肯定会遇到需要把.py脚本打包成.exe的情形,如此,至少有两方面的好处:第一,你的代码保密性更好 ...

  9. Python 浅拷贝copy()与深拷贝copy.deepcopy()

    首先我在这介绍两个新的小知识,要在下面用到.一个是函数 id() ,另一个是运算符 is.id() 函数就是返回对象的内存地址:is 是比较两个变量的对象引用是否指向同一个对象,在这里请不要和 == ...

  10. PHP json_encode 中文乱码

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code   在编码过程中.经常会用到json_encode来处理中文.但是.出现一个问题.中文 ...