推荐:React Native Flexi Radio Button - 灵活易用的单选按钮组件
    		       		warning:
    		            这篇文章距离上次修改已过448天,其中的内容可能已经有所变动。
    		        
        		                
                
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import FlexiRadioButton from 'react-native-flexi-radio-button';
 
export default class RadioButtonExample extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRadio: '',
    };
  }
 
  onRadioSelect = (selectedRadio) => {
    this.setState({ selectedRadio });
  };
 
  render() {
    const radioButtons = [
      { label: '选项一', value: '1' },
      { label: '选项二', value: '2' },
      { label: '选项三', value: '3' },
    ];
 
    return (
      <View style={styles.container}>
        <FlexiRadioButton
          dataSource={radioButtons}
          onSelectionChanged={this.onRadioSelect}
          selectedValue={this.state.selectedRadio}
        />
      </View>
    );
  }
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});这段代码演示了如何在React Native应用中使用Flexi Radio Button组件。首先,在构造函数中,我们初始化了组件的状态,用于存储选中的单选按钮的值。onRadioSelect方法用于更新所选按钮的状态。在render方法中,我们定义了单选按钮的数据源,并将方法和状态传递给Flexi Radio Button组件。这个例子简单明了地展示了如何使用这个组件,并在用户选择单选按钮时更新状态。
评论已关闭