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

1.在布局文件中为button添加onClick属性,Activity实现其方法
2.匿名内部类作为事件监听器类
3.内部类作为监听器
4.Activity本身作为事件监听器,实现onClickListener
5.外部类作为监听器
ButtonListenerActivity.class
public class ButtonListenerActivity extends AppCompatActivity implements View.OnClickListener{

    private Button bt_one,bt_two,bt_three,bt_four,bt_five;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_listener); bt_two = (Button) findViewById(R.id.bt_listener_two);
bt_three = (Button) findViewById(R.id.bt_listener_three);
bt_four = (Button) findViewById(R.id.bt_listener_four);
bt_five = (Button) findViewById(R.id.bt_listener_five); //方式二:匿名内部类作为事件监听器类
bt_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonListenerActivity.this,"方式二:匿名内部类",Toast.LENGTH_LONG).show();
}
}); //方式三:内部类作为事件监听器类
MyButton listener = new MyButton(this);
bt_three.setOnClickListener(listener); //方式四:Activity本身作为事件监听器,实现onClickListener
bt_four.setOnClickListener(this); //方式五:外部类作为事件监听器类
bt_five.setOnClickListener(new MyButtonListener(this));
} //1.方式一:在布局文件中为button添加onClick属性,Activity实现其方法
public void buttonClick(View view){
Toast.makeText(ButtonListenerActivity.this,"方式一:onClick",Toast.LENGTH_LONG).show();
} //方式四:Activity本身作为事件监听器,实现onClickListener--重写onClick()
@Override
public void onClick(View v) {
Toast.makeText(ButtonListenerActivity.this,"方式四:Activity本身作为事件监听器",Toast.LENGTH_LONG).show();
} //方式三:内部类作为事件监听器类
class MyButton implements View.OnClickListener{
private Context context;
public MyButton(Context context){
this.context = context;
}
@Override
public void onClick(View v) {
Toast.makeText(context,"方式三:内部类",Toast.LENGTH_LONG).show();
}
} }

  

activity_button_listener.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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:orientation="vertical"
tools:context="com.langdon.taiyang.androidtest.button.ButtonListenerActivity"> <Button
android:id="@+id/bt_listener_one"
android:text="方式一:onClick绑定"
android:onClick="buttonClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_listener_two"
android:text="方式二:匿名内部类"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_listener_three"
android:text="方式三:内部类监听"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_listener_four"
android:text="方式四:Activity本身实现监听事件"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_listener_five"
android:text="方式五:外部类作为监听"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

  MyButtonListener.class

public class MyButtonListener implements View.OnClickListener {
private Context context;
public MyButtonListener(Context context){
this.context = context;
}
@Override
public void onClick(View v) {
Toast.makeText(context,"方式五:外部类",Toast.LENGTH_LONG).show();
}
}

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. LISA介绍及其使用方法

    LISA是ARM公司开发的一款开源工具.在内核开发过程中,苦于无法针对修改内容进行一些量化或者可视化结果的测量,而无感.LISA对于模型调优,回归测试都有较强的支持. 什么是LISA? LISA是Li ...

  2. PPS传奇生死

    地址 :http://game.pps.tv/events/cqss_sign 调用encryptedString('password') RSA加密,通过function(a) 定义 window= ...

  3. 关于mybatis中mapper.xmlSQL语句书写的心得

    本文主要针对MySQL ---------------------------------------------------------------------------------------- ...

  4. python学习之day6,常用标准模块

    1.时间模块 time import time #时间戳转字符串格式 a = time.time() print(a) #打印时间戳 b = time.localtime(a) #把时间戳转换成时间对 ...

  5. Centos 7 使用jenkins 打包android项目

      1.配置java环境   在sun官网下载,解压后放入/usr/java下,并配置环境变量如 vi /etc/profile 在最底部加入如下配置(注:添加环境变量,都是在文件最底部添加)   J ...

  6. Autofac 依赖注入

    介绍 Autofac是一款IOC框架,很轻量级性能非常高,自动注入很给力. NuGet Autofac:Autofac控制反转容器核心 Autofac.MVC5:提供IDependencyResolv ...

  7. 【原创】自己动手写工具----XSmartNote [Beta 3.0]

    一.前面的话 在动笔之前,一直很纠结到底要不要继续完成这个工具,因为上次给它码代码还是一年多之前的事情,参考自己动手写工具----XSmartNote [Beta 2.0],这篇博文里,很多园友提出了 ...

  8. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  9. BootLoader的架构设计

    @注:黄色部分代表根据不同的开发板进行处理.

  10. [转载]El Capitan 中 SIP 介绍

    这两天大家纷纷将 OS X 系统升级到了 El Capitan,然后发现,一些注入的工具无法使用了,某些系统目录无法使用了,第三方未签名的 kext 无法加载了,问题一堆堆的.这是因为,Mac OS ...