Android圆角Button和点击效果的结合

1、用xml文件实现圆角
    a、在drawable文件夹下新建shape.xml文件,内如如下:
        <?xml version="1.0" encoding="UTF-8"?>
        <shape
           xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <!--Button正常状态下的背景色-->
            <solid android:color="#A3B1B2" />
            <!--设置圆角的半径-->
            <corners android:radius="10dip"/>
        </shape>
    b、再新建一个shape_press.xml
        <?xml version="1.0" encoding="UTF-8"?>
        <shape
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">
             <!--Button触摸状态下的背景色-->
             <solid android:color="#8C9899" />
              <!--设置圆角的半径-->
             <corners android:radius="10dip"/>
        </shape>
2、在java代码中设置Buttond的触摸事件,使用不同的xml资源实现点击效果
    public class ButtonOnTouchListener implements View.OnTouchListener {
         @Override
         public boolean onTouch(View v, MotionEvent event) {
             if(v.getId()==R.id.button1){
                Button b = (Button)v;
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    //触摸时,调用shape_press.xml资源
                    b.setBackgroundResource(R.drawable.shape_press);
                }else if(event.getAction()==MotionEvent.ACTION_UP){
                    //释放时,调用shape.xml资源
                    b.setBackgroundResource(R.drawable.shape);
                }
            }
          return false;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值