项目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://github.com/1165863642/ButtonDemo