React Native按下按钮呈现的效果

实现点击“在线客服“”和“设置“”一栏背景色由#ffffff变为#f0f2f5


<View style={styles.settingView}>


                                          <TouchableHighlight  onPress={()=>{}}
                                         onHideUnderlay={()=>{this.setState({ pressed1: false })}}
                                         onShowUnderlay={()=>{this.setState({ pressed1: true })}}
                                         underlayColor={'transparent'}>
                                              <View style={[styles.button,this.state.pressed1 ?{backgroundColor:'#f0f2f5'}:{backgroundColor:'#ffffff'}]}>

                          <Image source={require('./../image/icon/service.png')} style={{width:21,height:18,alignSelf:'center',backgroundColor:'transparent'}}/>
                          <Text style={{marginLeft:15,fontSize:15,alignSelf:'center',width:width-84}}>在线客服</Text>
                          <Image source={require('./../image3/right.png')} style={styles.rightly}/>
                           </View>
                         </TouchableHighlight>

                    <View style={styles.dividerview}>
                        <Text style={styles.divider}></Text>
                    </View>

                    <TouchableHighlight  onPress={()=>{}}
                                         onHideUnderlay={()=>{this.setState({ pressed2: false })}}
                                         onShowUnderlay={()=>{this.setState({ pressed2: true })}}
                                         underlayColor={'transparent'}>
                        <View style={[styles.button,this.state.pressed2 ?{backgroundColor:'#f0f2f5'}:{backgroundColor:'#ffffff'}]}>

                        <Image source={require('./../image/icon/setting.png')} style={{width:21,height:18,alignSelf:'center'}}/>
                        <Text style={{marginLeft:15,fontSize:15,alignSelf:'center',width:width-84}}>设置</Text>
                        <Image source={require('./../image3/right.png')} style={[styles.rightly]}/>
                    </View>
                        </TouchableHighlight>

                </View>

 const styles = StyleSheet.create({
settingView:
    {

        height:105,
        width:width,
        marginTop:20,
        // backgroundColor:'#FFFFFF',

    },
    button:
    {
        // flex:1,
        height:52,
        flexDirection:'row',
        paddingLeft:15,
        paddingRight:15,
        // justifyContent:'space-between'

    },
    dividerview: {
        flexDirection: 'row',
    },
    divider: {
        flex: 1,
        height: 1,
        backgroundColor: '#ECEDF1',
    },
    rightly:
    {
        width:18,
        height:18,
        alignSelf:'center',
        paddingRight:5,
        backgroundColor:'transparent'


    },
})
onPress={()=>{}}一定要有,要不然点击时没有效果。

如何区分什么时候是按下的,什么时候是按下松开的这就提上日程了。

处理这个问题需要请出React的State了。默认状态State是未按下(pressed为false),按下了改为pressed为true。就酱。

这需要用到TouchableHighlight的两个事件onShowUnderlay按下调用和onHideUnderlay,这个在按下松开后调用。 在这两个事件发生的时候修改state, 这样就会触发整个组件重绘。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值