当前位置: 代码迷 >> Android >> Android学习笔记(三)——按钮点击注册事件的四种写法
  详细解决方案

Android学习笔记(三)——按钮点击注册事件的四种写法

热度:72   发布时间:2016-04-28 01:38:40.0
Android学习笔记(3)——按钮点击注册事件的四种写法

搬运自本人博客,xge技术博客

http://www.xgezhang.com/android_button_onclick_4_ways.html

Android下,按钮点击事件是在开发过程中经常会写到的东西。这里总结一下常见的四种写法:

界面代码就是一个button控件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<RelativeLayoutxmlns: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.xgezhang.setonclick4ways.MainActivity">
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
 
</RelativeLayout>

1.通过匿名内部类实现监听类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
publicclass MainActivity extends Activity {
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        Button bt = (Button) findViewById(R.id.button1);
        bt.setOnClickListener(newOnClickListener() {
             
            @Override
            publicvoid onClick(View v) {
                // TODO Auto-generated method stub
                Toast tst = Toast.makeText(MainActivity.this,"way1", Toast.LENGTH_SHORT);
                tst.show();
            }
        });
    }
 
}

2.自定义一个个内部类,实现接口函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
publicclass MainActivity extends Activity {
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        Button bt = (Button) findViewById(R.id.button1);
        bt.setOnClickListener(newMyOnClickListener());
    }
             
    classMyOnClickListener implementsOnClickListener{
        @Override
        publicvoid onClick(View v) {
            // TODO Auto-generated method stub
            Toast tst = Toast.makeText(MainActivity.this,"way2", Toast.LENGTH_SHORT);
            tst.show();
        }
    }
}
            

上述两种方法麻烦之初在于,如果有很多的Button,那么就需要对每一个Button都写一个类,代码比较复杂。

3.让MainActivity实现接口函数,通过switch语句判断R.id,来对不同的Button按钮做出不同的响应。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
publicclass MainActivity extends Activity implements OnClickListener{
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        Button bt = (Button) findViewById(R.id.button1);
        bt.setOnClickListener(this);
    }
             
    @Override
    publicvoid onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        caseR.id.button1:
            Toast tst = Toast.makeText(MainActivity.this,"way3", Toast.LENGTH_SHORT);
            tst.show();
            break;
        default:
            break;
        }
    }
}

4.在XML文件中“显式指定按钮的onClick属性,点击按钮时会利用反射的方式调用对应Activity中的click()方法:

XML文件添加一行:

?
1
android:onClick="onClick"

对应Java代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
publicclass MainActivity extends Activity{
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    publicvoid onClick(View v) {
        // TODO Auto-generated method stub
            Toast tst = Toast.makeText(this,"way4", Toast.LENGTH_SHORT);
            tst.show();
    }
}

当然,第四种方法也可以通过switch语句判断哪一个Button被点击。
在实际开发过程中,使用最多的是第三种方法。

  相关解决方案