React Native Button实现

使用React Native实现Button的效果:

0. 概述

使用React Native的TouchableHighlight组件包装Text、Image或其他组件。因为TouchableHighlight有onPress回调方法,可以处理点击事件。

TouchableHighlight API

1. Basic Button实现

使用TouchableHightlight的属性/方法:

onPress:处理点击事件

underlayColor:点击时TouchableHightlight的颜色

实现代码:

// 普通按钮效果。
//点击时只改变背景色而不改变字体等样式,只需使用 TouchableHighlight 中的 underlayColor 属性
class BasicButton extends Component {
    render(){
        return(
            <TouchableHighlight
                style={styles.exit}
                underlayColor="#d9d9d9"
                onPress={() => {
                    console.log('Press Basic Button!');
                }}>
                <Text style={styles.exittext}>
                    Basic Button
                </Text>
            </TouchableHighlight>
        );
    }
}

2. Change Style Button
使用TouchableHighlight的属性/方法:

onPress:处理点击事件

underlay:点击时underlay的颜色

onHideUnderlay:当underlay隐藏时会调用此方法。可以用来处理没有Touch按钮时的样式。

onShowUnderlay:当underlay显示时会调用此方法。可以用了处理Touch按钮时的样式。

实现代码:

// 点击时改变背景色同时改变按钮的字体颜色
// 需要使用TouchableHighlight的onHideUnderlay和onShowUnderlay属性
const RED_COLOR = 'red';
const WHITE_COLOR = 'white';
 
class ChangeStyleButton extends Component {
    constructor(props) {
        super(props);
        this.state = { pressStatus: false };
    }
 
    _onHideUnderlay(){
        this.setState({ pressStatus: false });
    }
 
    _onShowUnderlay(){
        this.setState({ pressStatus: true });
    }
 
    render(){
        return(
            <TouchableHighlight
                onHideUnderlay={this._onHideUnderlay.bind(this)}
                onPress={() => {
                    console.log('Press Change Style Button');
                }}
                onShowUnderlay={this._onShowUnderlay.bind(this)}
                style={[styles.exit, this.state.pressStatus ? {backgroundColor: RED_COLOR} : {backgroundColor: WHITE_COLOR}]}
                underlayColor='red'>
                <Text style={[styles.exittext, this.state.pressStatus ? {color: WHITE_COLOR} : {color: RED_COLOR}]}>
                    Change Style Button
                </Text>
            </TouchableHighlight>
        );
    }
}

3. 单选按钮
实现代码:

class RedioButton extends Component {
 
    constructor(props) {
        super(props);
        this.state = {
            isAllSelected: false,
            isNotDealSelected: true,
            isDealedSelected: false,
        }
    }
 
    // 更新"全部/未处理/已处理"按钮的状态
    _updateBtnSelectedState(currentPressed, array) {
        if (currentPressed === null || currentPressed === 'undefined' || array === null || array === 'undefined') {
            return;
        }
 
        let newState = {...this.state};
 
        for (let type of array) {
            if (currentPressed == type) {
                newState[type] ? {} : newState[type] = !newState[type];
                this.setState(newState);
            } else {
                newState[type] ? newState[type] = !newState[type] : {};
                this.setState(newState);
            }
        }
    }
 
    // 返回设置的button
    _getButton(style, selectedSate, stateType, buttonTitle, count) {
        let BTN_SELECTED_STATE_ARRAY = ['isAllSelected', 'isNotDealSelected', 'isDealedSelected'];
        return(
            <View style={[style, selectedSate ? {backgroundColor: '#32a7f5'} : {}]}>
                <Text
                    style={[styles.button, selectedSate ? {color: 'white'} : {}]}
                    onPress={ () => {this._updateBtnSelectedState(stateType, BTN_SELECTED_STATE_ARRAY)}}>
                    {buttonTitle}({count})
                </Text>
            </View>
        );
    }
 
    render(){
        return(
            <View style={styles.buttonlayout}>
                {this._getButton(styles.buttonleft, this.state.isAllSelected, 'isAllSelected', 'Btn_1', 10)}
                <View style={styles.buttondivideline}></View>
                {this._getButton(null, this.state.isNotDealSelected, 'isNotDealSelected', 'Btn_1', 20)}
                <View style={styles.buttondivideline}></View>
                {this._getButton(styles.buttonright, this.state.isDealedSelected, 'isDealedSelected', 'Btn_1', 30)}
            </View>
        );
    }
}

4. 上面案例中用的Style
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  exit: {
      width: 380,
      height: 50,
      marginTop: 30,
      alignItems: 'center',
      borderColor: 'red',
      borderWidth: 1,
      borderStyle: 'solid',
      borderRadius: 5,
  },
  exittext: {
      height: 50,
      fontSize: 18,
      textAlignVertical: 'center',
  },
  buttonlayout: {
      height: 30,
      marginTop: 30,
      alignSelf: 'center',
      flexDirection: 'row',
      alignItems: 'center',
      borderColor: '#32a7f5',
      borderWidth: 1,
      borderStyle: 'solid',
      borderRadius: 8,
  },
  buttonleft: {
      borderTopLeftRadius: 8,
      borderBottomLeftRadius: 8,
  },
  buttonright: {
      borderTopRightRadius: 8,
      borderBottomRightRadius: 8,
  },
  button: {
      height: 30,
      textAlign: 'center',
      textAlignVertical: 'center',
      paddingLeft: 10,
      paddingRight: 10,
  },
  buttondivideline: {
      width: 1,
      height: 30,
      backgroundColor: '#32a7f5',
      flexDirection: 'column',
  },
});


原文:https://blog.csdn.net/henglei1/article/details/52651917 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native是一种用于构建跨平台移动应用的框架,它可以使用JavaScript和React技术来开发原生移动应用。下面是React Native的一些常见用法: 1. 创建一个新的React Native项目: 使用React Native CLI命令行工具创建一个新的React Native项目,例如: ``` npx react-native init MyApp ``` 2. 编写组件: 在项目中编写React Native组件,可以使用class组件或函数式组件来定义自定义组件。组件可以包含UI元素、样式和交互逻辑等。 3. 导入和使用内置组件: React Native提供了许多内置的UI组件和API,可以通过导入和使用它们来构建应用。例如,可以使用View、Text、Image、Button等组件来创建用户界面。 4. 样式和布局: 使用StyleSheet.create()方法创建样式对象,并将其应用于组件的style属性中。可以使用Flexbox布局模型来进行灵活的布局。 5. 处理用户输入和交互: 可以使用内置的触摸事件(如onPress、onLongPress等)来处理用户的输入和交互。还可以使用手势识别器(如PanResponder)来实现更复杂的手势操作。 6. 导航和路由: React Navigation是一个常用的第三方库,用于实现导航和路由功能。可以使用它来创建导航栏、选项卡、侧边菜单等导航组件。 7. 调用原生模块和API: 如果需要访问设备的原生功能和API,可以使用React Native提供的桥接机制来调用原生模块和API。这允许开发者编写原生代码并与React Native应用进行交互。 8. 调试和测试: React Native提供了一些调试和测试工具,如React Native Debugger、Reactotron等,可以帮助开发者调试和测试应用。 以上是React Native的一些常见用法,但还有很多其他的功能和工具可供使用。建议查阅React Native官方文档和相关教程来深入了解和学习React Native的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值