Button点击事件的几种实现方式
# 方式一:XML属性的onClick
在java类中定义好点击事件要执行的方法,在布局文件xml中调用 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vip.veblonwang.myapplication.MainActivity">
<Button
android:id="@+id/btnDoSomething"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="share"
android:onClick="doSomething"/><!-- 这句是关键,点击时执行MainActivity中的doSomething方法 -->
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
MainActivity .java
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//方法必须定义为public
public void doSomething(View view) {
// TODO
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 方式二:匿名内部类
在每次使用时新实现一个OnClickListener接口
public class MainActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_todo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 方式三:该页面类实现OnClickListener接口
public class MainActivity extends Activity implements View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_todo);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_todo:
// TODO
break;
default:
break;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 {
private Button btn_todo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbtn_fifth = findViewById(R.id.btn_todo);
//findViewById(R.id.btn_third).setOnClickListener(new InnerOnClickListener());
InnerOnClickListener onClickListener = new InnerOnClickListener();
findViewById(R.id.btn_third).setOnClickListener(onClickListener);
}
// 内部类
private class InnerOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO
}
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
或者把类中的点击事件看作是一个对象,新建一个类来实现这些事件
public class MainActivity extends Activity{
private Button btn;
private ClickListener clickListener = new ClickListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btnDoSomething);
btn.setOnClickListener(clickListener);
}
private class ClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 方式五:OnClickListener接口作为属性
public class MainActivity extends Activity{
private Button btn;
private View.OnClickListener clickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_todo);
btn.setOnClickListener(clickListener);
clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO
}
};
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
编辑 (opens new window)
上次更新: 2022/03/27, 22:58:50