1. 程式人生 > >Android Button四種點擊事件和長按事件

Android Button四種點擊事件和長按事件

package ear pat get nts click cal mas dwg

項目XML代碼

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation
="vertical" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_marginTop="10dp" 14 android:onClick="doClick" 15 android:text="xml添加doClock"/> 16 17 <
Button 18 android:id="@+id/button2" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:layout_marginTop="10dp" 22 android:text="匿名內部類點擊"/> 23 24 <Button 25 android:id="@+id/button3" 26 android:layout_width
="match_parent" 27 android:layout_height="wrap_content" 28 android:layout_marginTop="10dp" 29 android:text="自定義點擊事件"/> 30 31 <Button 32 android:id="@+id/button4" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:layout_marginTop="10dp" 36 android:text="繼承方式"/> 37 38 <Button 39 android:id="@+id/button5" 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" 42 android:layout_marginTop="10dp" 43 android:text="長按點擊事件"/> 44 45 </LinearLayout>

JAVA代碼:

 1 package com.example.a11658.buttondemo;
 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 implements View.OnClickListener {
10     Button button2, button3, button4, button5;
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16 
17         initView();
18     }
19 
20     private void initView() {
21         button2 = findViewById(R.id.button2);
22         button3 = findViewById(R.id.button3);
23         button4 = findViewById(R.id.button4);
24         button5 = findViewById(R.id.button5);
25 
26         button2.setOnClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 //匿名內部類實現點擊事件
30                 Toast.makeText(MainActivity.this, "匿名內部類的點擊事件", Toast.LENGTH_SHORT).show();
31             }
32         });
33 
34         button3.setOnClickListener(new MyListener());
35         button4.setOnClickListener(this);
36         button5.setOnLongClickListener(new View.OnLongClickListener() {
37             @Override
38             public boolean onLongClick(View v) {
39                 Toast.makeText(MainActivity.this, "長按事件", Toast.LENGTH_SHORT).show();
40                 return false;
41             }
42         });
43     }
44 
45     public void doClick(View view) {
46         //xml通過onClick實現點擊事件
47         Toast.makeText(this, "xml點擊實現", Toast.LENGTH_SHORT).show();
48     }
49 
50     @Override
51     public void onClick(View v) {
52         //繼承方式
53         Toast.makeText(this, "繼承點擊", Toast.LENGTH_SHORT).show();
54     }
55 
56     class MyListener implements View.OnClickListener {
57 
58         @Override
59         public void onClick(View v) {
60             //自定義方式
61             Toast.makeText(MainActivity.this, "自定義點擊事件", Toast.LENGTH_SHORT).show();
62         }
63     }
64 }
備註:Button數量不多的情況下推薦使用第二種,匿名內部類的方式實現;反之則推薦使用第四種,Activity繼承View.OnClickListener實現

代碼鏈接:https://pan.baidu.com/s/1hlkrcybgMUrS2rrslnDwGg

Android Button四種點擊事件和長按事件