Android 实现button点击效果的两种方法

所谓的button点击效果就是:button原来是蓝色的,按下button后,button变成了红色,手抬起来,button又变回了蓝色。

想要实现这种效果在我看来一共有两种方法。

下面,我先介绍第一种方法,比较笨重的,实现了OnTouchListener的方法。

MainActivity.java文件

public class MainActivity extends AppCompatActivity {

    private Button btn01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn01 = (Button) findViewById(R.id.btn01);
        btn01.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    btn01.setBackgroundColor(Color.RED);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    btn01.setBackgroundResource(R.mipmap.ic_launcher);
                    Toast.makeText(MainActivity.this,"超级变变变~~~~",Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }
}

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yuan.test01.MainActivity">

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>

</RelativeLayout>

这种方法的原理是通过OnTouchListener监听用户的手势,进而判断button什么时候该改变样式。
但这样写有一个问题,这里仅仅是一个button需要增加点击效果还好,那么,button多了起来会怎么样呢?
没错,随着button的增多,我们需要不停得监听每一个button的OnTouch事件,这是很笨重的。

基于上面的情况,我推荐大家使用第二种方法。

首先,我们需要在drawable文件夹创建3个xml文件。
分别是:
normal.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#dd794949"></solid>
</shape>

pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#dd0046c7"></solid>
</shape>

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/normal" android:state_pressed="false"/>
    <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
</selector>

然后,修改一下 activity_main文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yuan.test01.MainActivity">

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"/>

</RelativeLayout>

最后,把MainActiviti.java文件中的init()方法注释掉

package com.example.yuan.test01;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initView();
    }

    private void initView() {
        btn01 = (Button) findViewById(R.id.btn01);
        btn01.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    btn01.setBackgroundColor(Color.RED);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    btn01.setBackgroundResource(R.mipmap.ic_launcher);
                    Toast.makeText(MainActivity.this,"超级变变变~~~~",Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }
}

这样一来,不管有多少个button,只要设置这行代码就可以了android:background=”@drawable/button_selector”

是不是比第一种方法灵活多了

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值