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 ...
随机推荐
- 【JavaWeb学习】文件的上传和下载
一.文件上传 1.1.概述 实现web开发中的文件上传功能,需完成如下二步操作: 在web页面中添加上传输入项 在servlet中读取上传文件的数据,并保存到本地硬盘中 如何在web页面中添加上传输入 ...
- web 前端常用组件【07】弹出层 Layer
web 项目中总是需要弹出框,来让用户进行下一步的操作. 大到弹出另外一个页面,小到弹出提示.确认等. 经手几个项目,还是感觉 Layer 用起来比较的轻松,你能想到的 Layer 都能帮你做到. 感 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)
前言:最近一段时间在学习MVC源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ...
- C#之发送邮件【模板】+【封装】ZJ版
PS: 为了弥补上篇博客的不足,正好周六闲着没事.所以进行优化下,来个终结版 功能实现:模板发送+自指定邮箱发送+解耦 总体预览如下: 各代码如下:(代码略多,所以都折叠了) 前台; @{ Layou ...
- 扩展RadioButtonListFor和CheckBoxListFor
在我们做正常的MVC的开发中,一些基本的控件已经够用了,但是有时候我们需要用到库里面没有的一些控件,比如RadioButtonListFor和CheckBoxListFor这类的列表控件,在MVC库里 ...
- go database/sql sql-driver/mysql 操作
这里使用的是github.com/Go-SQL-Driver/MySQL, 所以需要下载一个github.com/Go-SQL-Driver/MySQL 引入 database/sql 和 githu ...
- map 函数----filter函数
# map 函数 l = (1,2,4,5,6,7,8,9,) print(list(map(lambda x:x**2,l)))#使用list类型((map函数(lambda 匿名函数定义x值:x* ...
- 浅析 Android 的窗口
来源:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=555&fromuid=6 一.窗口的概念 在开发过程中,我们经常会 ...
- 获取文件mime类型
检测文件类型 finfo_file (PHP >= 5.3.0, PECL fileinfo >= 0.1.0) 修改php.ini,将extension=php_fileinfo.dll ...
- bitset用法总结
b.any() b中是否存在置为1的二进制位? b.none() b中不存在置为1的二进制位吗? b.count() b中置为1的二进制位的个数 b.size() b中二进制位的个数 b[pos] 访 ...