React-Native Picker与PickeriOS

Picker选择器,可以自由创建Item用来选择
主要属性:

selectedValue:选择的值,可以是value属性值
onValueChange:方法,提供两个参数,一个是被选中项的value属性,一个是索引位置
:为picker的item

创建一个picker:

this.setState({language: lang})}>
                
                
                
                
                
                
                
                
            

PickerIOS针对于iOS版本的picker
主要属性没变
创建一个PickerIOS:

CAR_MAKES_AND_MODELS参数可以在完整代码中查看
this._iosValue11(itemValue)}
                >
                    {
                        Object.keys(CAR_MAKES_AND_MODELS).map((carmake)=>(
                             
                    ))
                    }
                

完整代码:

import React, { Component } from 'react';
import {
    AppRegistry,
    Picker,
    PickerIOS,
    View
} from 'react-native';
var CAR_MAKES_AND_MODELS = {
    amc: {
        name: 'AMC',
        models: ['AMX', 'Concord', 'Eagle', 'Gremlin', 'Matador', 'Pacer'],
    },
    alfa: {
        name: 'Alfa-Romeo',
        models: ['159', '4C', 'Alfasud', 'Brera', 'GTV6', 'Giulia', 'MiTo', 'Spider'],
    },
    aston: {
        name: 'Aston Martin',
        models: ['DB5', 'DB9', 'DBS', 'Rapide', 'Vanquish', 'Vantage'],
    },
};
export default class RNPickerView extends Component {
    constructor(props){
        super(props);
        this.state = {
            language: 'java',
            selectCount: 0,
            carMake: 'amc',
        }
    }
    _pickerView = ()=>{
        return(
            this.setState({language: lang})}>
                
                
                
                
                
                
                
                
            
        )
    };
    _iosValue11 = (item)=> {
        console.log('_iosValue11' + item);
        this.setState({
            carMake: item
        })
    };
    _iosValue22 = (item)=> {
        console.log('_iosValue22' + item);
        this.setState({
            selectCount: item
        })
    };
    _pickerIOS = ()=>{
        let make = CAR_MAKES_AND_MODELS[this.state.carMake];
        let selectIndex = make.name + ' ' + make.models[this.state.selectCount];
        return(
            
                this._iosValue11(itemValue)}
                >
                    {
//这里是有map函数变量了CAR_MAKES_AND_MODELS对象中的keys值
                        Object.keys(CAR_MAKES_AND_MODELS).map((carmake)=>(
                             
                    ))
                    }
                
                this._iosValue22(itemValue)}>
                    {
//这里map遍历的是models数组,modelName为数组的value,modelIndex数组位置

CAR_MAKES_AND_MODELS[this.state.carMake].models.map((modelName, modelIndex)=>(
                            
                        ))
                    }
                
            
        )
    };
    render() {
        return(
            
                {this._pickerView()}
                {this._pickerIOS()}
            
        )
    }
}
AppRegistry.registerComponent('RNPickerView', ()=> RNPickerView);

效果图:

React-Native Picker与PickeriOS_第1张图片
CA479814-0E51-47D5-8FC7-C99A714EF90F.png

你可能感兴趣的:(React-Native Picker与PickeriOS)