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

被折叠的 条评论
为什么被折叠?



