简单聊一下Android中Button的五种监听事件:

  1. 1.在布局文件中为button添加onClick属性,Activity实现其方法
    2.匿名内部类作为事件监听器类
    3.内部类作为监听器
    4.Activity本身作为事件监听器,实现onClickListener
    5.外部类作为监听器
  1. ButtonListenerActivity.class
  1. public class ButtonListenerActivity extends AppCompatActivity implements View.OnClickListener{
  2.  
  3. private Button bt_one,bt_two,bt_three,bt_four,bt_five;
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_button_listener);
  7.  
  8. bt_two = (Button) findViewById(R.id.bt_listener_two);
  9. bt_three = (Button) findViewById(R.id.bt_listener_three);
  10. bt_four = (Button) findViewById(R.id.bt_listener_four);
  11. bt_five = (Button) findViewById(R.id.bt_listener_five);
  12.  
  13. //方式二:匿名内部类作为事件监听器类
  14. bt_two.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. Toast.makeText(ButtonListenerActivity.this,"方式二:匿名内部类",Toast.LENGTH_LONG).show();
  18. }
  19. });
  20.  
  21. //方式三:内部类作为事件监听器类
  22. MyButton listener = new MyButton(this);
  23. bt_three.setOnClickListener(listener);
  24.  
  25. //方式四:Activity本身作为事件监听器,实现onClickListener
  26. bt_four.setOnClickListener(this);
  27.  
  28. //方式五:外部类作为事件监听器类
  29. bt_five.setOnClickListener(new MyButtonListener(this));
  30. }
  31.  
  32. //1.方式一:在布局文件中为button添加onClick属性,Activity实现其方法
  33. public void buttonClick(View view){
  34. Toast.makeText(ButtonListenerActivity.this,"方式一:onClick",Toast.LENGTH_LONG).show();
  35. }
  36.  
  37. //方式四:Activity本身作为事件监听器,实现onClickListener--重写onClick()
  38. @Override
  39. public void onClick(View v) {
  40. Toast.makeText(ButtonListenerActivity.this,"方式四:Activity本身作为事件监听器",Toast.LENGTH_LONG).show();
  41. }
  42.  
  43. //方式三:内部类作为事件监听器类
  44. class MyButton implements View.OnClickListener{
  45. private Context context;
  46. public MyButton(Context context){
  47. this.context = context;
  48. }
  49. @Override
  50. public void onClick(View v) {
  51. Toast.makeText(context,"方式三:内部类",Toast.LENGTH_LONG).show();
  52. }
  53. }
  54.  
  55. }

  

  1. activity_button_listener.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context="com.langdon.taiyang.androidtest.button.ButtonListenerActivity">
  8.  
  9. <Button
  10. android:id="@+id/bt_listener_one"
  11. android:text="方式一:onClick绑定"
  12. android:onClick="buttonClick"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. <Button
  16. android:id="@+id/bt_listener_two"
  17. android:text="方式二:匿名内部类"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content" />
  20. <Button
  21. android:id="@+id/bt_listener_three"
  22. android:text="方式三:内部类监听"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content" />
  25. <Button
  26. android:id="@+id/bt_listener_four"
  27. android:text="方式四:Activity本身实现监听事件"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content" />
  30. <Button
  31. android:id="@+id/bt_listener_five"
  32. android:text="方式五:外部类作为监听"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content" />
  35. </LinearLayout>

  MyButtonListener.class

  1. public class MyButtonListener implements View.OnClickListener {
  2. private Context context;
  3. public MyButtonListener(Context context){
  4. this.context = context;
  5. }
  6. @Override
  7. public void onClick(View v) {
  8. Toast.makeText(context,"方式五:外部类",Toast.LENGTH_LONG).show();
  9. }
  10. }

Android中Button的五种监听事件的更多相关文章

  1. Android中Preference的使用以及监听事件分析

    在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...

  2. Unity中Button按钮的触发监听事件

    第一种方式:需要把自己添加的Button按钮属性(Inspector)中的(Button)onclick添加方法. public void BtnCreteClick() { Debug.Log(&q ...

  3. Button的五种点击事件

    1.内部类方式 class MyOnClickListener implements View.OnClickListener{ /** * Called when a view has been c ...

  4. jquery四种监听事件的区别

    最近找工作被问到了jquery有哪些事件监听,都有什么区别,忽然有点想不起来了... 然后上网上查看了相关的资料,总结一下,方便大家查看,也方便自己复习! 1.bind()方法: bind(type, ...

  5. Android中常用的五种数据存储方式

    第一种: 使用SharedPreferences存储数据 适用范围: 保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配置信息(如是否打开音效.是否使用震动效果.小 ...

  6. 【Android】NavigationView头部点击监听事件

    AndroidStudio给出的模板里面只有列表点击事件,即实现OnNavigationItemSelectedListener中的onNavigationItemSelected方法,根据item的 ...

  7. Android EditText获取焦点和失去焦点监听事件

    实现方法也很简单.那就是绑定OnFocusChangeListener事件.实现onFocusChange(View v, boolean hasFocus) 方法.第二个参数就是判断得到焦点或失去焦 ...

  8. javascript事件有哪些?javascript的监听事件

    事件类型: 1.界面事件 onload:描述文档,图片,css已经frame,object加载完毕时触发,window.onload window.onload = function(){ //代表图 ...

  9. Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)

    第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...

随机推荐

  1. VUE---Missing space before function parentheses

    解决方法:

  2. Stream与byte[]与Image与string

    public byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null)) { using (MemoryStre ...

  3. canvas-图片翻转

    图片90度翻转 在canvas中插入图片需先加载图片(利用Image对象);加载完成后再执行操作drawImage(obj,x,y,w,h) 插入图片的坐标宽高等值 <!DOCTYPE html ...

  4. [转]使用Enumeration和Iterator遍历集合类

    原文地址:http://www.cnblogs.com/xwdreamer/archive/2012/05/30/2526268.html 前言 在数据库连接池分析的代码实例中,看到其中使用Enume ...

  5. MyBatis中collection (一对一,一对多)

    MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...

  6. Deconvolution Using Theano

    Transposed Convolution, 也叫Fractional Strided Convolution, 或者流行的(错误)称谓: 反卷积, Deconvolution. 定义请参考tuto ...

  7. 使用 win+r 命令行打开我们的桌面应用(处女座的福音)

    首先新建一个文件夹,名为quickapp,然后在地址栏复制文件目录地址,进入系统高级设置,修改系统环境变量Path,双击后选择新建,输入quickapp文件目录地址,确认保存. 如何修改path变量? ...

  8. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  9. 利用animation和text-shadow纯CSS实现loading点点点的效果

    经常在网上看到loading状态时的点点点的动态效果,自己也用JS写了一个,思路是使用一个计数参数,然后在需要添加点的元素后面利用setInterval一个一个加点,当计数到3时,把点变为一个--写完 ...

  10. 纪念BLives 1.0版本发布

    历时两个多月的时间,BLives程序1.0发布,在开发程序期间自己经历了很多,考试,恋爱,学业,自己很纠结 很伤心,有时候很无助,为了让自己有事干,我在考试备考期间去设计程序- -#,虽然程序设计的一 ...