Android:View点击事件监听的四种方式

时间:2021-01-24 22:34:57

(一)在布局文件中为控件设置onClick属性的指定点击方法

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.example.myapplication.MainActivity">
12 
13     <Button
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:id="@+id/b1"
17         android:onClick="b1"
18         />
19 </RelativeLayout>

 

 1 package com.example.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Toast;
 7 
 8 public class MainActivity extends AppCompatActivity {
 9 
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13     }
14     public void b1(View view){
15         Toast.makeText(this,"b1",Toast.LENGTH_LONG).show();
16     }
17 }

 

(二)创建一个内部类,实现OnClickListener接口,并重写onClick()方法,之后用setOnClickListener()方法绑定内部类

(setOnClickListener()方法,需要获取一个要执行类(动作))

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.example.myapplication.MainActivity">
12 
13     <Button
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:id="@+id/b1"
17 
18         />
19 </RelativeLayout>

 

 1 package com.example.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.Toast;
 8 
 9 public class MainActivity extends AppCompatActivity {
10 
11     private Button b1;
12     protected void onCreate(Bundle savedInstanceState) {//Bundle savedInstanceState 当前实例的一种状态

13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main);

15 b1=(Button) findViewById(R.id.b1);//获取事件源

16 b1.setOnClickListener(new b1());//将事件源与相应的相应绑定
17

18 }

19class b1 implements View.OnClickListener{

20publicvoid onClick(View view){ 21 Toast.makeText(MainActivity.this,"b1",Toast.LENGTH_LONG).show();

22

23 }

24 }

25

26 }

 

(三)思路同上,但不在书写内部类,直接由当前类实现OnClickListener接口,并重写onClick()方法

 1 import android.support.v7.app.AppCompatActivity;
 2 import android.os.Bundle;
 3 import android.view.View;
 4 import android.widget.Button;
 5 import android.widget.Toast;
 6 
 7 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 8 
 9     private Button b1;
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13 b1=(Button) findViewById(R.id.b1);
14         b1.setOnClickListener(this);
15 
16         }
17 public void onClick(View view)
18         {
19         Toast.makeText(MainActivity.this,"b1",Toast.LENGTH_LONG).show();
20 
21         }
22 
23 
24         }

 

(四)创建匿名内部类,即在进行监听时直接创建一个OnClickListener

package com.example.myapplication;

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  {

    private Button b1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.b1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"b1",Toast.LENGTH_LONG).show();
            }
        });

    }
}