Android中,按钮Button的点击事件处理方法有多种,以下我总结下自己在使用过程中遇到的几种情况。
1.直接在布局文件里面定义Button点击事件的响应方法(适用于多个Button)。
此是唯一不需要在代码里面再次对Button的点击事件进行绑定的方法。
在布局文件里面定义点击响应方法如下:
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onListener"
android:layout_below="@id/txt"
android:text="Button 01" />
<Button
android:id="@+id/button02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onListener"
android:layout_below="@id/button01"
android:text="button 02" />
</RelativeLayout>
我们可以看到每个Button里面都有这样的一个属性:
android:onClick="onListener"
当然这个onListener方法还需要我们在代码里面自己定义,代码的实现如下:
package com.ramos.ljx2010hxj.buttononclickdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onListener(View v){
switch(v.getId()){
case R.id.button01:
Toast.makeText(getApplicationContext(), "This is button 01", Toast.LENGTH_SHORT).show();
break;
case R.id.button02:
Toast.makeText(getApplicationContext(), "This is button 02", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "No id found", Toast.LENGTH_SHORT).show();
}
}
}//end class MainActivity
2.Activity实现OnClickListener接口(适用于多个Button)。
由于Activity实现了OnClickListener接口,所以此时会自动在代码里面生成一个OnClick方法,我们只要实现这个方法就可以了。
此时布局文件如下:
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:text="Button 01" />
<Button
android:id="@+id/button02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button01"
android:text="button 02" />
</RelativeLayout>
代码如下:
package com.ramos.ljx2010hxj.buttononclickdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button btn01;
private Button btn02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button)findViewById(R.id.button01);
btn01.setOnClickListener(this);
btn02 = (Button)findViewById(R.id.button02);
btn02.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button01:
Toast.makeText(getApplicationContext(), "This is button 01",
Toast.LENGTH_SHORT).show();
break;
case R.id.button02:
Toast.makeText(getApplicationContext(), "This is button 02",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "No id found",
Toast.LENGTH_SHORT).show();
}
}
}// end class MainActivity
3.直接在setOnClickListener的时候new 一个OnClickListener(适用于单个Button)。
布局文件同2.
代码如下:
package com.ramos.ljx2010hxj.buttononclickdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn01;
private Button btn02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button)findViewById(R.id.button01);
btn01.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This is button 01",
Toast.LENGTH_SHORT).show();
}
});
btn02 = (Button)findViewById(R.id.button02);
btn02.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This is button 02",
Toast.LENGTH_SHORT).show();
}
});
}
}// end class MainActivity
4.新建一个内部类,实现OnClickListener接口。
布局文件同2.
代码如下:
package com.ramos.ljx2010hxj.buttononclickdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn01;
private Button btn02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button) findViewById(R.id.button01);
btn01.setOnClickListener(new BtnOnClickListener());
btn02 = (Button) findViewById(R.id.button02);
btn02.setOnClickListener(new BtnOnClickListener());
}
class BtnOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button01:
Toast.makeText(getApplicationContext(), "This is button 01",
Toast.LENGTH_SHORT).show();
break;
case R.id.button02:
Toast.makeText(getApplicationContext(), "This is button 02",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "No id found", Toast.LENGTH_SHORT).show();
}
}
}
}// end class MainActivity
5.定义一个OnClickListener类型的变量(适用于多个Button)。
布局文件同2.
代码如下:
package com.ramos.ljx2010hxj.buttononclickdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn01;
private Button btn02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button) findViewById(R.id.button01);
btn01.setOnClickListener(listener);
btn02 = (Button) findViewById(R.id.button02);
btn02.setOnClickListener(listener);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button01:
Toast.makeText(getApplicationContext(), "This is button 01",
Toast.LENGTH_SHORT).show();
break;
case R.id.button02:
Toast.makeText(getApplicationContext(), "This is button 02",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "No id found",
Toast.LENGTH_SHORT).show();
}
}
};
}// end class MainActivity