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 ...
随机推荐
- VUE---Missing space before function parentheses
解决方法:
- Stream与byte[]与Image与string
public byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null)) { using (MemoryStre ...
- canvas-图片翻转
图片90度翻转 在canvas中插入图片需先加载图片(利用Image对象);加载完成后再执行操作drawImage(obj,x,y,w,h) 插入图片的坐标宽高等值 <!DOCTYPE html ...
- [转]使用Enumeration和Iterator遍历集合类
原文地址:http://www.cnblogs.com/xwdreamer/archive/2012/05/30/2526268.html 前言 在数据库连接池分析的代码实例中,看到其中使用Enume ...
- MyBatis中collection (一对一,一对多)
MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...
- Deconvolution Using Theano
Transposed Convolution, 也叫Fractional Strided Convolution, 或者流行的(错误)称谓: 反卷积, Deconvolution. 定义请参考tuto ...
- 使用 win+r 命令行打开我们的桌面应用(处女座的福音)
首先新建一个文件夹,名为quickapp,然后在地址栏复制文件目录地址,进入系统高级设置,修改系统环境变量Path,双击后选择新建,输入quickapp文件目录地址,确认保存. 如何修改path变量? ...
- uva10635 LIS
Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...
- 利用animation和text-shadow纯CSS实现loading点点点的效果
经常在网上看到loading状态时的点点点的动态效果,自己也用JS写了一个,思路是使用一个计数参数,然后在需要添加点的元素后面利用setInterval一个一个加点,当计数到3时,把点变为一个--写完 ...
- 纪念BLives 1.0版本发布
历时两个多月的时间,BLives程序1.0发布,在开发程序期间自己经历了很多,考试,恋爱,学业,自己很纠结 很伤心,有时候很无助,为了让自己有事干,我在考试备考期间去设计程序- -#,虽然程序设计的一 ...