Button点击事件的四种实现

本文介绍了在Android中实现Button点击事件的四种方法:1)使用匿名内部类,2)Activity继承View.OnClickListener,3)自定义点击事件监听类,4)通过xml里的反射调用方法。提供详细代码示例及资源下载链接。

博客导航

首先在xml文档建立下面代码,这是放四个button上去。

 

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.button_demo.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="第一个按钮"
        android:textSize="25dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/button_selector"
        android:layout_marginLeft="40dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="第二个按钮"
        android:textSize="25dp"
        android:layout_below="@+id/button1"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_selector"

        android:layout_marginLeft="40dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="第三个按钮"
        android:textSize="25dp"
        android:layout_below="@+id/button2"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_selector"
        android:layout_marginLeft="40dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="第四个按钮"
        android:textSize="25dp"
        android:layout_below="@+id/button3"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_selector"
        android:onClick="onClick4"
        android:layout_marginLeft="40dp"/>
</RelativeLayout>

 

在代码里实现四种button点击事件。

 

package com.example.button_demo;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button bt1, bt2, bt3, bt4;

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

        bt1 = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        bt3 = (Button) findViewById(R.id.button3);
        bt4 = (Button) findViewById(R.id.button4);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "第一个按钮", Toast.LENGTH_SHORT).show();
            }
        });
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(new myClickListener());



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                Toast.makeText(MainActivity.this, "第二个按钮", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;

        }
    }

    public class myClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button3:
                    Toast.makeText(MainActivity.this, "第三个按钮", Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;

            }
        }
    }

    public void onClick4(View v) {
        switch (v.getId()) {
            case R.id.button4:
                Toast.makeText(MainActivity.this, "第四个按钮", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;

        }
    }

}

 

 

 

 

 

1.匿名内部类实现

这个方法相信很多人比较熟悉了

 

<span style="white-space:pre">	</span>bt1 = (Button) findViewById(R.id.button1);
        
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "第一个按钮", Toast.LENGTH_SHORT).show();
            }
        });

 

 

 

2.Activity继承View.OnClickListener,由Activity实现OnClick(View view)方法

 

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        bt2 = (Button) findViewById(R.id.button2);
        bt2.setOnClickListener(this);

   @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                Toast.makeText(MainActivity.this, "第二个按钮", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;

        }
    }


这种方式在按钮多的时候比较方便。

 

3.自定义点击事件监听类

bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(new myClickListener());

public class myClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button3:
                    Toast.makeText(MainActivity.this, "第三个按钮", Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;

            }
        }
    }

 

 

4.在xml里利用反射调用方法

 

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="第四个按钮"
        android:textSize="25dp"
        android:layout_below="@+id/button3"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_selector"
        style="color:#ff0000;">android:onClick="onClick4"
        android:layout_marginLeft="40dp"/>

 

 public void onClick4(View v) {
        switch (v.getId()) {
            case R.id.button4:
                Toast.makeText(MainActivity.this, "第四个按钮", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;

        }
    }

在xml里设置

android:onClick="onClick4"

调用代码里 onClick4(View v)的方法。这个方法名可以自己自行定义。
 

 

示例代码下载:http://download.youkuaiyun.com/detail/loongago/9548428
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值