React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
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)的更多相关文章
- React Native 组建之IOS和Android通用抽屉
/** * Sample React Native App * https://github.com/facebook/react-native * @flow *npm:https://www.np ...
- 封装 React Native 原生组件(iOS / Android)
封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...
- 如何用 React Native 创建一个iOS APP?
诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...
- 如何用 React Native 创建一个iOS APP?(二)
我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...
- React Native之通知栏消息提示(android)
React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...
- 如何用 React Native 创建一个iOS APP?(三)
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...
- React native 之设置IOS的图标,名称和启动图(下篇文章会讲到RN的android的相关设置)
1.首先,app的名称: 如图所示:我的工程名叫BOOk 在BOOk下面的info.plist的文件里设置app的相关信息:比如Bundle name就是设置APP的名称 2.App的图标:(这里注意 ...
- React Native热更新(iOS)-Pushy
React Native的出现,使的开发iOS代码出现了更便捷的方式.由于RN是使用脚本语言编写的,实现了"解释执行"的方式,而这种执行方式的修改只需替换脚步即可,不需要重新发布程 ...
- React Native项目集成iOS原生模块
今天学习一下怎么在React Native项目中集成iOS原生模块,道理和在iOS原生项目中集成React Native模块类似.他们的界面跳转靠的都是iOS原生的UINavigationContro ...
随机推荐
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...
- Navicat 链接mysql 显示 Clinet dose not support authentication protocol request by server ;consider upgrading MySQL client
1 在命令窗口 输入mysql -uroot -p 首先通过cmd进入mysql 2 更改加密方式 mysql> ALTER USER 'root'@'localhost' IDENTIFIE ...
- pom文件miss artifact com.sun:tools:jar:1.5.0:system问题
问题现象: 导入新的maven项目时,有时候pom.xml文件会提示一个错误信息:Missing artifact com.sun:tools:jar:1.5.0:system 问题原因: maven ...
- maven 标签classifier 研究一下
研究一下maven的标签: <dependency> <groupId>io.netty</groupId> <artifactId>netty-tcn ...
- selenium之选项卡管理
在使用selenium管理浏览器的时候遇到一些小问题,现将这些小问题作一下总结: (1)browser是什么? from selenium import webdriver browser = web ...
- 【js】使用javascript 实现静态网页分页效果
<!DOCTYPE html> <html> <head> <meta http-equiv="content-Type" content ...
- Ubuntu16.04环境安装jenkins
前提:系统必须安装jdk 如果没有安装可通过如下命令安装: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt update sudo apt ins ...
- GOjs版本2.0去除水印
在go.js找到:"7eba17a4ca3b1a8346" 找到这个关键字,将: 改成:a.br=function(){return true}即可.
- 【Codeforces 339】Xenia and Bit Operations
Codeforces 339 D 题意:给定\(2^n\)个数字,现在把它们进行如下操作: 相邻的两个数取\(or\) 相邻的两个数取\(xor\) 以此类推,直到剩下一个数. 问每次修改一个数字, ...