Android UI控件详解-Button(按钮)点击事件的5种写法

package com.bdqn.button;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

/**
 * 
 * @author TXF
 * 
 * Button点击事件的5种写法
 * 
 */
public class MainActivity extends Activity implements OnClickListener {
	private Button mbtn1, mbtn2, mbtn3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 加载主布局文件
		setContentView(R.layout.activity_main);

		// 第一种写法(定义内部类实现Button的点击事件)
		mbtn1 = (Button) findViewById(R.id.btn1);
		mbtn1.setOnClickListener(new MyListener());

		// 第二种写法(定义匿名内部类实现Button的点击事件)
		mbtn2 = (Button) findViewById(R.id.btn2);
		mbtn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity();
			}
		});

		// 第三种写法:让Activity实现点击事件的接口(适用于多个Button)
		// 1.让MainActivity实现OnClickListener接口;
		// 2.复写onClick方法;
		// 3.在onClick方法里面用Switch,case来判断不同Button的id从而响应不同的点击事件
		mbtn3 = (Button) findViewById(R.id.btn3);
		mbtn3.setOnClickListener(this);

	}

	// 第四种写法:在布局文件中绑定不同名的点击方法(就是 android:onClick="click04",后面的属性值不同)
	// 不需要findViewById,也不需要setOnClickListener,只需要写多个方法就行
	public void click04(View view) {
		startActivity();
	}

	/*
	 * public void click01(View view) { }
	 * public void click02(View view) { }
	 * ....
	 */

	// 第一种:定义内部类实现Button的点击事件
	private class MyListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			startActivity();
		}

	}

	// 这是第三种方法复写的onClick方法
	@Override
	public void onClick(View view) {
		startActivity();

	}

	// 第五种:在布局文件中定义同名的点击方法(就是 android:onClick="click",后面的属性值都一样,都为click)
	// 通过switch,case来判断不同Button的Id,这种方法最简单。以后开发遇到多个Button的时候用这种方法比较简单
	public void click(View view) {
		switch (view.getId()) {
		case R.id.btn5:
			startActivity();
			break;
		// ...
		default:
			break;
		}
	}

	// 开启第二个界面的方法
	private void startActivity() {
		startActivity(new Intent(MainActivity.this, OtherActivity.class));
	}

}

xml布局

<LinearLayout 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:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="内部类定义点击事件" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匿名内部类定义点击事件" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="实现类定义点击事件" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click04"
        android:text="布局文件中定义不同名的点击事件" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="布局文件中定义同名的点击事件" />

</LinearLayout>
效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值