Android中Button的五种监听事件
简单聊一下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的五种监听事件的更多相关文章
- Android中Preference的使用以及监听事件分析
在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...
- Unity中Button按钮的触发监听事件
第一种方式:需要把自己添加的Button按钮属性(Inspector)中的(Button)onclick添加方法. public void BtnCreteClick() { Debug.Log(&q ...
- Button的五种点击事件
1.内部类方式 class MyOnClickListener implements View.OnClickListener{ /** * Called when a view has been c ...
- jquery四种监听事件的区别
最近找工作被问到了jquery有哪些事件监听,都有什么区别,忽然有点想不起来了... 然后上网上查看了相关的资料,总结一下,方便大家查看,也方便自己复习! 1.bind()方法: bind(type, ...
- Android中常用的五种数据存储方式
第一种: 使用SharedPreferences存储数据 适用范围: 保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配置信息(如是否打开音效.是否使用震动效果.小 ...
- 【Android】NavigationView头部点击监听事件
AndroidStudio给出的模板里面只有列表点击事件,即实现OnNavigationItemSelectedListener中的onNavigationItemSelected方法,根据item的 ...
- Android EditText获取焦点和失去焦点监听事件
实现方法也很简单.那就是绑定OnFocusChangeListener事件.实现onFocusChange(View v, boolean hasFocus) 方法.第二个参数就是判断得到焦点或失去焦 ...
- javascript事件有哪些?javascript的监听事件
事件类型: 1.界面事件 onload:描述文档,图片,css已经frame,object加载完毕时触发,window.onload window.onload = function(){ //代表图 ...
- Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)
第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...
随机推荐
- Ext分页实现(前台与后台)
Ext分页实现(前台与后台)Spring+Mybatis 一.项目背景 关于Ext的分页网上有很多博客都有提到,但是作为Ext新手来说,并不能很容易的在自己的项目中得以应用.因为,大多数教程以及博客基 ...
- Freemarker中空值 null的处理++++定义数组
http://blog.java-zone.org/archives/800.html <#list listBlogPost as blogPost> </#list> 如果 ...
- HTML5射击类游戏----【地球保卫战】
在线DEMO地址:打开: 游戏截图: 就不贴代码了, 因为代码太多了, 大概写一下这个游戏实现思路和一些实现: 游戏一共有三关, 每一关都有一个大Boss, Boss比较好杀,主要各种外星飞 ...
- php对应js math.random
<?php function random($min = 0, $max = 1) { return $min + mt_rand()/mt_getrandmax()*($max-$mi ...
- Nginx编译安装(Centos)
前言 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大 ...
- BZOJ2117: [2010国家集训队]Crash的旅游计划
裸点分,点分树每层维护有序表,查询二分,复杂度$O(nlog^3n)$. #include<bits/stdc++.h> #define M (u+v>>1) #define ...
- DOM
DOM:Document Object Model 文档对象模型文档:html页面文档对象:页面中的元素文档对象模型:定义为了能够让程序(js)去操作页面中的元素DOM会把文档看作是一棵树docume ...
- Eclipse中.calsspath文件解析
来自帅气的Sublime Text
- bootstrap1
让bootstarp3 支持ie的兼容模式: 支持浏览器的响应式布局: 是指网页既可以用在pc上,也可以用在手机上, 而且不需要修改源文件. bootstrap包括: css文件, 只需要加载: cs ...
- shell test用法
1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达式都为真 test 表达式1 –o 表达式 ...