如何在React Native

时间:2015-08-08 05:19:57

标签: ios reactjs react-native

我正在将React代码转换为React Native。所以我需要实现单选按钮。

8 个答案:

答案 0 :(得分:46)

您只需使用准系统RN即可轻松模仿单选按钮。这是我使用的一个简单实现。根据需要调整尺寸,颜色等。它看起来像这样(带有不同的色调和一些文字)。在顶部添加TouchableOpacity可将其转换为可执行操作的按钮。

enter image description here

function RadioButton(props) {
  return (
      <View style={[{
        height: 24,
        width: 24,
        borderRadius: 12,
        borderWidth: 2,
        borderColor: '#000',
        alignItems: 'center',
        justifyContent: 'center',
      }, props.style]}>
        {
          props.selected ?
            <View style={{
              height: 12,
              width: 12,
              borderRadius: 6,
              backgroundColor: '#000',
            }}/>
            : null
        }
      </View>
  );
}

答案 1 :(得分:5)

有一个名为react-native-radio-buttons的反应原生组件可以完成您需要的一些工作:

enter image description here

答案 2 :(得分:5)

这是创建radioButtons的另一种方法(Source,这要归功于php的逐步频道)

方法1

constructor(props) {
    super(props);
    this.state = {
        radioBtnsData: ['Item1', 'Item2', 'Item3'],
        checked: 0
    }
}

import { View, TextInput, TouchableOpacity } from 'react-native';

{this.state.radioBtnsData.map((data, key) => {
    return (
        <View key={key}>
            {this.state.checked == key ?
                <TouchableOpacity style={styles.btn}>
                    <Image style={styles.img} source={require("./img/rb_selected.png")}/>
                    <Text>{data}</Text>
                </TouchableOpacity>
                :
                <TouchableOpacity onPress={()=>{this.setState({checked: key})}} style={styles.btn}>
                    <Image style={styles.img} source={require("./img/rb_unselected.png")} />
                    <Text>{data}</Text>
                </TouchableOpacity>
            }
        </View>
    )
})}

const styles = StyleSheet.create({
    img:{
        height:20,
        width: 20
    },
    btn:{
        flexDirection: 'row'
    }
});

将图片放在img文件夹中的图片下方

enter image description here enter image description here

方法2

为新开发者精心设计的LaneRettig ex

感谢Lane Rettig

constructor(props){
    super(props);
    this.state = {
      radioSelected: 1
    }
  }


  radioClick(id) {
    this.setState({
      radioSelected: id
    })
  }

  render() {

    const products = [{
      id: 1
    },
    {
      id: 2
    },
    {
      id: 3
    }];

    return (
      products.map((val) => {
        return (
          <TouchableOpacity key={val.id} onPress={this.radioClick.bind(this, val.id)}>
            <View style={{
              height: 24,
              width: 24,
              borderRadius: 12,
              borderWidth: 2,
              borderColor: '#000',
              alignItems: 'center',
              justifyContent: 'center',
            }}>
              {
                val.id == this.state.radioSelected ?
                  <View style={{
                    height: 12,
                    width: 12,
                    borderRadius: 6,
                    backgroundColor: '#000',
                  }} />
                  : null
              }
            </View>
          </TouchableOpacity>
        )
      })
    );
  }

答案 3 :(得分:1)

我使用Checkbox中的react-native创建单选按钮。请参考下面的代码。

constructor(props){
    super(props);
    this.state = {radioButton:'value1'};
}
render(){
    return(
        <View>
            <CheckBox 
                title='value1'
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked={this.state.radioButton === 'value1'}
                onPress={() => this.setState({radioButton: 'value1'})}
                ></CheckBox>
            <CheckBox 
                title='value2'
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked={this.state.radioButton === 'value2'}
                onPress={() => this.setState({radioButton: 'value2'})}
                ></CheckBox> 
            <CheckBox 
                title='value3'
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked={this.state.radioButton === 'value3'}
                onPress={() => this.setState({radioButton: 'value3'})}
                ></CheckBox> 
            <CheckBox 
                title='value4'
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked={this.state.radioButton === 'value4'}
                onPress={() => this.setState({radioButton: 'value4'})}
                ></CheckBox> 
            <CheckBox 
                title='value5'
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked={this.state.radioButton === 'value5'}
                onPress={() => this.setState({radioButton: 'value5'})}
                ></CheckBox>  

        </View>
    );
}

答案 4 :(得分:1)

这是我使用功能组件的单选按钮解决方案。

注意-我已将图像用于选中和未选中的单选图标

import React, {useState} from 'react';
import {View, Text, StyleSheet, TouchableOpacity, Image} from 'react-native';

const Radio = () => {
  const [checked, setChecked] = useState(0);
  var gender = ['Male', 'Female'];
  return (
    <View>
      <View style={styles.btn}>
        {gender.map((gender, key) => {
          return (
            <View key={gender}>
              {checked == key ? (
                <TouchableOpacity style={styles.btn}>
                  <Image
                    style={styles.img}
                    source={require('../images/radio_Checked.jpg')}
                  />
                  <Text>{gender}</Text>
                </TouchableOpacity>
              ) : (
                <TouchableOpacity
                  onPress={() => {
                    setChecked(key);
                  }}
                  style={styles.btn}>
                  <Image
                    style={styles.img}
                    source={require('../images/radio_Unchecked.png')}
                  />
                  <Text>{gender}</Text>
                </TouchableOpacity>
              )}
            </View>
          );
        })}
      </View>
      {/* <Text>{gender[checked]}</Text> */}
    </View>
  );
};

const styles = StyleSheet.create({
  radio: {
    flexDirection: 'row',
  },
  img: {
    height: 20,
    width: 20,
    marginHorizontal: 5,
  },
  btn: {
    flexDirection: 'row',
    alignItems: 'center',
  },
});

export default Radio;

答案 5 :(得分:0)

也许您可以将其设置为圆形按钮,例如单选按钮或其他样式: example Tabs or same concept

const period = [
      {
        key: 1,
        name : '1 Month',
        value : 1,

      },
      {
        key : 2,
        name : '12 Month',
        value : 12,

      }
    ];

    constructor(props) {
        super(props);
        this.state = {
          value: 0,
        };
      }

    onChangeTabs = (tabs) => {

        this.setState({
          value : tabs,

        })

      }

    renderTabs = () => {
        return period.map(item => (
          <TouchableWithoutFeedback
            key={item.key}
            value={item.value}
            onPress={this.onChangeTabs.bind(this, item.value)}
          >
            <View style={this.state.value == item.value ? styles.periodActive : styles.period}>
              <Text style={styles.periodText}>{item.name}</Text>
            </View>
          </TouchableWithoutFeedback>
        ));
      };


    const styles = StyleSheet.create({
    period: {
        borderRadius: 5,
        borderColor: '#fff',
        borderWidth: 1,

        marginHorizontal: 5,
      },
      periodActive: {
        backgroundColor: '#333',
      },
    });

答案 6 :(得分:0)

import RadioForm, { RadioButton, RadioButtonInput, RadioButtonLabel } from 'react-native-simple-radio-button';
const radioProps = [
  { label: 'Male', value: false },
  { label: 'Female, value: true }
];



<View style={{ marginTop: 30 }}>
                <RadioForm
                  buttonColor={gray}
                  buttonSize={12}
                  radioStyle={{paddingTop:25}}
                  selectedButtonColor="#000000"
                  radio_props={radioProps}
                  initial={0}
                  animation={false}
                  onPress={(value) => setGender(value)}
                />
              </View>

答案 7 :(得分:-2)

您可以使用react-native-radio-input。 它非常易于使用。

import RadioGroup,{Radio} from "react-native-radio-input";
.
.
.
//Receive the checked value (ES6 syntax)
getChecked = (value) => {
// value = our checked value
alert(value)
}
 <RadioGroup getChecked={this.getChecked}>
  <Radio iconName={"lens"} label={"The first option"} value={"A"} />
  <Radio iconName={"lens"} label={"The first option"} value={"B"} />
  <Radio iconName={"lens"} label={"I need numbers"} value={1} />
  <Radio label={"Is IconName Optional?"} value={"Yes"} />
  <Radio label={"Show Sentence Value"} value={"This is a Sentence"} />
</RadioGroup>
.
.