Buatton点击事件的几种写法

匿名内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt = (Button) findViewById(R.id.bt1);
//1.匿名内部类
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("匿名内部类", "点击事件");
}
});
}

定义内部类,实现OnClickListener接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class MainActivity extends Activity{

private Button bt;

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

bt = (Button) findViewById(R.id.bt);

bt.setOnClickListener(new MyListener());
}

//定义内部类,实现OnClickListene接口
class MyListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("定义内部类,实现OnClickListene接口", "点击事件");
}
}
}

定义的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MainActivity extends Activity{

private Button bt;

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

bt = (Button) findViewById(R.id.bt);

myListener();
}

//定义构造方法
private void myListener() {
// TODO Auto-generated method stub
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("定义构造方法", "点击事件");
}
});
}
}

用Activity实现OnClickListener接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MainActivity extends Activity implements OnClickListener {

private Button bt;

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

bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(this);
}
  
  
  //用Activity实现OnClickListener接口
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("用Activity实现OnClickListener接口", "点击事件");
}

}

指定Button的onClick的属性

布局文件

1
2
3
4
5
6
<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="点击事件" />

Java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MainActivity extends Activity {

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

}

//5指定onClick属性方式
public void click(View v) {
// TODO Auto-generated method stub
Log.i("指定onClick属性方式","点击事件");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class MainActivity extends Activity {

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

}

//指定onClick属性方式
//传人的View对象,就是用户按下的那个按钮对象
public void click(View v) {
// TODO Auto-generated method stub

//获取组件的资源id
int id = v.getId();
switch (id) {
case R.id.bt1:
Log.i("指定onClick属性方式","bt1点击事件");
break;
case R.id.bt2:
Log.i("指定onClick属性方式","bt2点击事件");
break;
case R.id.bt3:
Log.i("指定onClick属性方式","bt3点击事件");
break;
case R.id.bt4:
Log.i("指定onClick属性方式","bt4点击事件");
break;

default:
break;
}
}
}