ReactNative之自定义选中按钮

作者: 袁峥 | 来源:发表于2017-05-16 16:28 被阅读551次

前言

眼看很多公司都开始尝试使用ReactNative,达到跨平台开发,最近也写了很多文章,希望让更多想了解的同学快速上手ReactNative.

如果喜欢我的文章,可以关注我微博:袁峥Seemygo

ReactNative之自定义选中按钮

  • 在RN开发中,自带的按钮组件非常不好用,没有选中状态,更加不可以自定义样式,要达到开发需求只能自定义按钮了。

自定义选中按钮

  • 暴露以下属性

    static propTypes = {
        // 普通状态
        title:PropTypes.string,
        imageUri:PropTypes.string,
        titleStyle:PropTypes.object,
        imageStyle:PropTypes.object,

        // 高亮状态
        selectedImageUri:PropTypes.string,
        selectedTitleStyle:PropTypes.object,

        // 监听点击
        onPressIn:PropTypes.func,
        onPressOut:PropTypes.func,

        // 选中状态
        selected:PropTypes.bool,

        // 按钮样式
        buttonStyle:PropTypes.object

    };
  • 实现代码
/**
 * Created by ithinkeryz on 2017/5/16.
 */
/**
 * Created by ithinkeryz on 2017/5/15.
 */

import React, { Component,PropTypes} from 'react';

import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    TouchableOpacity

} from 'react-native';

export default class CommonSelectButton extends Component {
    
    static propTypes = {
        // 普通状态
        title:PropTypes.string,
        imageUri:PropTypes.string,
        titleStyle:PropTypes.object,
        imageStyle:PropTypes.object,

        // 高亮状态
        selectedImageUri:PropTypes.string,
        selectedTitleStyle:PropTypes.object,

        // 监听点击
        onPressIn:PropTypes.func,
        onPressOut:PropTypes.func,

        // 选中状态
        selected:PropTypes.bool,

        // 按钮样式
        buttonStyle:PropTypes.object

    };

    constructor(props){
        super(props);

        this.state = {
            selected:this.props.selected
        }
    }

    render() {
        return (
            <TouchableOpacity style={[styles.buttonStyle,this.props.buttonStyle]}
                              onPressIn={()=>{
                                  if (this.props.onPressIn){
                                      this.props.onPressIn(this);
                                  }

                              }}
                              onPressOut={()=>{
                                  if (this.props.onPressOut){
                                      this.props.onPressOut(this);
                                  }
                              }}
                              activeOpacity={this.props.selectedImageUri || this.props.selectedTitleStyle?0.9:0.3}
            >

                {/*文字*/}
                {this.props.title?<Text style={[this.props.titleStyle,this.props.selected?this.props.selectedTitleStyle:null]}>{this.props.title}</Text>:null}

                {/*头像*/}
                {this.props.imageUri?<Image source={{uri:this.state.selected && this.props.selectedImageUri?this.props.selectedImageUri:this.props.imageUri}} style={[styles.imageStyle,this.props.imageStyle]}/> : null}

            </TouchableOpacity>
        );
    }


}

var styles = StyleSheet.create({
    buttonStyle:{
        backgroundColor:'white',
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center'
    },
    imageStyle:{
        marginLeft:3
    }
});
  • 如何使用自定义选中按钮
<CommonSelectButton imageUri='mine-sun-icon-click'
                                selectedImageUri='mine-moon-icon'
                                imageStyle={{width:20,height:20}}
                                onPressIn={(button)=>{
                                    var selected = button.state.selected;
                                    selected = !selected;
                                    button.setState({
                                        selected:selected
                                    })
                                }}
            />
  • 实现效果
普通状态.png 选中状态.png

相关文章

网友评论

  • 鬼谷门生:袁老师你好,看了你的扩展视频,Rxswift你讲了一个tableView的案例,那个案例你的数据是本地数据,那么要是网络请求下来的数据怎么给viewModel的属性赋值才能让tableView时时监听到数据源的改变呢,刚刚学习swift,有时间希望你解答下谢谢!
    074509232f53:@鬼谷门生 那你是小马哥学员咯,有问题找袁先生当面问多好
    鬼谷门生:@laugh_许 扩展的视频是加密的 一机一码
    074509232f53:@鬼谷门生 求问你是在哪看的视频,袁先生的声音好听,能让我一听就懂,视频在哪尼

本文标题:ReactNative之自定义选中按钮

本文链接:https://www.haomeiwen.com/subject/povlxxtx.html