selection下拉框_React Native的单选、复选、下拉框组件的使用备忘

这篇博客介绍了如何在React Native中实现单选、复选和下拉框组件的使用。对于单选,使用了'react-native-radio-buttons'库;复选则依赖于'react-native-check-box';而下拉框功能通过'react-native-modal-dropdown'实现。文章提供了详细的安装步骤和代码示例。
摘要由CSDN通过智能技术生成

1. 单选

安装:

npm i react-native-radio-buttons --save

代码:

import React, { Component } from 'react'

import {TouchableWithoutFeedback, Text, View, TouchableHighlight, TextInput, CheckBox } from 'react-native'

import { RadioButtons } from 'react-native-radio-buttons'

export default class App4 extends Component {

constructor(props){

super(props)

this.state={gender:'男'}

}

_genderChangHandler=gender=>{

this.setState({gender})

}

_renderOption=(option, selected, onSelect, index)=>{

const style = selected ? { fontWeight: 'bold',color:'red'} : {};

return (

{option}

);

}

render() {

const options = ["男","女", "其它"];

return (

options={ options }

onSelection={ this._genderChangHandler }

selectedOption={this.state.gender }

renderOption={ this._renderOption }

renderContainer={optionNodes=> {optionNodes} }

/>

您本人的性别是:{this.state.gender}

);

}

}

2. 复选:

安装:

npm i react-native-check-box --save

代码:

import React, { Component } from 'react'

import { Text, View } from 'react-native'

import CheckBox from 'react-native-check-box'

export default class App5 extends Component {

constructor(props) {

super(props)

this.state = { favs: ['swim'] }

}

_favChangeHandler = fav => {

let favs = this.state.favs.slice(0)

let index = favs.indexOf(fav)

if (index >= 0) favs.splice(index, 1)

else favs.push(fav)

this.setState({favs})

}

render() {

return (

style={{ flex: 1, padding: 20 }}

onClick={() =>this._favChangeHandler('swim')}

isChecked={this.state.favs.includes('swim')}

rightText={"游泳"}

/>

style={{ flex: 1, padding: 20 }}

onClick={() => this._favChangeHandler('foot')}

isChecked={this.state.favs.includes('foot')}

rightText={"足球"}

/>

你的爱好是:{this.state.favs.join(",")}

)

}

}

3. 下拉选择:

安装:

npm i react-native-modal-dropdown -save

代码:

import React, { Component } from 'react'

import { Text, View } from 'react-native'

import ModalDropdown from 'react-native-modal-dropdown';

export default class App6 extends Component {

_majorChangeHandler=(index,item)=>{

console.log(index,item)

}

render() {

let majors=["计算机科学","软件工程","移动云计算"]

return (

defaultValue="请选择您的专业..."

dropdownTextStyle={{fontSize:22,color:'blue'}}

textStyle={{fontSize:30,color:'red'}}

dropdownTextHighlightStyle={{backgroundColor:"red",color:'yellow'}}

options={majors}/>

您的专业是:

)

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值