React Native开发之常用第三方控件

来源:互联网 发布:华罗庚的优化法时间 编辑:程序博客网 时间:2024/06/04 19:55

前言

http://blog.csdn.net/xiangzhihong8/article/details/53968573

React Native出来一年多了,受到各大开发人员的喜爱,但是由于只是专注于View层的开发,因此在很多深层次上还需要结合原生app做一定的兼容,还有就是现在好多控件,如Android中已是系统的控件的sidemenu、checkbox、gridview等,这些在React native中 系统是没有给我们提供的,这时候就借助了第三方开源的力量。 
那么我们今天说说在React Native项目开发中常见的一些第三方库。

常见的第三方库

组件篇

CheckBox(多选按钮)

react-native-check-box CheckBox 
基本用法:

 <CheckBox     style=     onClick={()=>this.onClick(data)}     isChecked={data.checked}     leftText={leftText} />;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

当然我们也可以自定义样式,主要是对选中和未选中的样式做修改:

renderCheckBox(data) {    var leftText = data.name;    return (        <CheckBox            style=            onClick={()=>this.onClick(data)}            isChecked={data.checked}            leftText={leftText}            checkedImage={<Image source={require('../../page/my/img/ic_check_box.png')} style={this.props.theme.styles.tabBarSelectedIcon}/>}            unCheckedImage={<Image source={require('../../page/my/img/ic_check_box_outline_blank.png')} style={this.props.theme.styles.tabBarSelectedIcon}/>}        />);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

RadioButton(单选按钮)

react-native-flexi-radio-button 
使用也很简单,就是在中嵌套下就行:

 <RadioGroup                   onSelect = {(index, value) => this.onSelect(index, value)}                >                     <RadioButton value={'item1'} >                         <Text>This is item #1</Text>                     </RadioButton>                    <RadioButton value={'item2'}>                         <Text>This is item #2</Text>                     </RadioButton>                     <RadioButton value={'item3'}>                         <Text>This is item #3</Text>                    </RadioButton>                </RadioGroup>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

sidemenu (侧滑栏)

react-native-side-menu 
使用:

<SideMenu menu={menu}>        <ContentView/>      </SideMenu>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

splashscreen

react-native-splash-screen 
使用也很简单,就是添加一个闪屏的xml 
这里写图片描述

imagepicker

这个组件帮助我们选取图片和调用相机等,这个组件同时支持photo和video,也就是照片和视频都可以用这个组件实现。 
用法:

 import ImagePickerManager from ‘NativeModules‘;var options = {  title: ‘Select Avatar‘, // 选择器的标题,可以设置为空来不显示标题  cancelButtonTitle: ‘Cancel‘,  takePhotoButtonTitle: ‘Take Photo...‘, // 调取摄像头的按钮,可以设置为空使用户不可选择拍照  chooseFromLibraryButtonTitle: ‘Choose from Library...‘, // 调取相册的按钮,可以设置为空使用户不可选择相册照片  customButtons: {    ‘Choose Photo from Facebook‘: ‘fb‘, // [按钮文字] : [当选择这个按钮时返回的字符串]  },  mediaType: ‘photo‘, // ‘photo‘ or ‘video‘  videoQuality: ‘high‘, // ‘low‘, ‘medium‘, or ‘high‘  durationLimit: 10, // video recording max time in seconds  maxWidth: 100, // photos only默认为手机屏幕的宽,高与宽一样,为正方形照片  maxHeight: 100, // photos only  allowsEditing: false, // 当用户选择过照片之后是否允许再次编辑图片};ImagePickerManager.showImagePicker(options, (response) => {  console.log(‘Response = ‘, response);  if (response.didCancel) {    console.log(‘User cancelled image picker‘);  }  else if (response.error) {    console.log(‘ImagePickerManager Error: ‘, response.error);  }  else if (response.customButton) {    // 这是当用户选择customButtons自定义的按钮时,才执行    console.log(‘User tapped custom button: ‘, response.customButton);  }  else {    // You can display the image using either data:    if (Platform.OS === ‘android‘) {        source = {uri: response.uri, isStatic: true};    } else {        source = {            uri: response.uri.replace(‘file://‘, ‘‘),            isStatic: true        };    }    this.setState({      avatarSource: source    });  }});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

然后在页面展示的时候:

<Image source={this.state.avatarSource} style={styles.uploadAvatar} />
  • 1
  • 1

说到这里,我们要说一下另一个控件picker

picker-Android

Picker就是ReactNative界的Spinner,其常用的属性有:

  • onValueChange 这个方法在方法在选择Picker某一项时调用 可传两个参数 选择的value和position
  • selectedValue 这个属性是选择的值
  • enabled 设置是否可点击 Android属性
  • mode 设置样式 Android属性 dropdown下拉样式和dialog弹窗样式 默认是dialog
  • prompt 设置Picker标题 Android属性 并且只有是mode为dialog才会显示
  • itemStyle 设置每一项的样式 iOS属性

用法:

/** * Created by Administrator on 2016/9/7. */import React, {Component} from 'react';import {    AppRegistry,    View,    Picker,} from 'react-native';class PickerG extends Component {    constructor(porp) {        super(porp);        this.state= {            selectedValue: ''        }    }    render() {        return (            <Picker                //Picker样式 dialog弹窗样式默认  dropdown显示在下边                // mode = {'dropdown'}                //显示选择内容                selectedValue={this.state.selectedValue}                //选择内容时调用此方法                onValueChange={(value)=>this.setState({selectedValue: value})}                //设置Title 当设置为dialog时有用                prompt={'请选择'}            >                <Picker.Item label='Android' value='android'/>                <Picker.Item label='IOS' value='ios'/>                <Picker.Item label='ReactNative' value='reactnative'/>            </Picker>        )    }}module.exports = PickerG;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

easy-toast

react-native-easy-toast 
这个组件兼容了Android和iOS的toast,使用也很简单。 
用法:

render() {         return (             <View style={styles.container}>                 ...                 <Toast ref="toast"/>             </View>         ); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

最后在需要调用的地方:

this.refs.toast.show('hello world!');
  • 1
  • 1

其他的一些库

选项卡 
各种漂亮的小组件 
按钮 
输入框表单验证 
https://github.com/gcanti/tcomb-form-native 
https://github.com/FaridSafi/react-native-gifted-form 
https://github.com/bartonhammond/snowflake 
炫酷效果的 TextInput 
https://github.com/halilb/react-native-textinput-effects 
https://github.com/zbtang/React-Native-TextInputLayout 
聊天表情 
地图 
动画 
加载动画 
日历 
可多选的Listview

附:http://www.jianshu.com/p/e7b8bb13c9b8

0 0
原创粉丝点击