Android 按钮监听器我常用的两种方法
来源:互联网 发布:pubmed数据库首页 编辑:程序博客网 时间:2024/06/06 04:24
Android开发中监听器的实现有三种方法,对于初学者来说,能够很好地理解这三种方法,将能更好地增进自己对android中监听器的理解。
一、什么是监听器。
监听器是一个存在于View类下的接口,一般以On******Llistener命名,实现该接口需要复写相应的on****(View v)方法(如onClick(View v))。
二、监听器的三种实现方法
方法二:实现匿名内部类。这种方法适合只希望对监听器进行一次性使用的情况,在该代码块运行完毕之后,该监听器也就不复存在了。
bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"you have clicked Button1",Toast.LENGTH_SHORT).show(); } });
方法三:利用布局文件中的onClick属性,并在实现文件中实现该方法。注意的是这里的方法名应该和布局文件中onClick属性的方法名相同,该方法必须是public方法。
// 方法三,注意需要public方法 public void onButtonClick (View view){ Toast.makeText(MainActivity.this,"you have clicked Button3",Toast.LENGTH_SHORT).show(); }}
在布局文件中的设置
<Button android:layout_below="@id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="Button3" android:onClick="onButtonClick"/>
以上就是android中实现监听器的三种方法。
下面是整个项目的完整代码
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.jeffrey.listener.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:text="OnClickListener" /> <Button android:layout_below="@id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" android:id="@+id/button1"/> <Button android:layout_below="@id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" android:id="@+id/button2"/> <Button android:layout_below="@id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="Button3" android:onClick="onButtonClick"/></RelativeLayout>
实现文件:
package com.example.jeffrey.listener;import android.content.DialogInterface;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1 = (Button)findViewById(R.id.button1);//对应方法二 Button bt2 = (Button)findViewById(R.id.button2);//对应方法一 bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"you have clicked Button1",Toast.LENGTH_SHORT).show(); } }); bt2.setOnClickListener(new MyListener()); } class MyListener implements View.OnClickListener{ @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show(); } } // 或者,这里是创建一个OnClickListener 的对象,与上面的直接复写接口有异曲同工之妙 private View.OnClickListener MyListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show(); } }; // 方法三,注意需要public方法 public void onButtonClick (View view){ Toast.makeText(MainActivity.this,"you have clicked Button3",Toast.LENGTH_SHORT).show(); }}
输出结果:当点击相应按钮时会出现“you have clicked Button?”的提示。
总结:三种方法可以说各有好处。第一种方法是新手入门必学。相对于第一种方法,后来两种方法在android开发中是比较常见的,也是大神们经常推荐使用的方法。至于自己想要使用那种方法,就看自己兴趣了。
阅读全文
0 0
- Android 按钮监听器我常用的两种方法
- Android中为Button注册监听器最常用的两种方法
- Android按钮实现的两种方法
- 14 Android android 按钮效果的两种实现方法
- android按钮监听器的写法
- Android 按钮效果的两种实现方法
- Android 按钮效果的两种实现方法
- Android开发 点击按钮切换背景的两种方法
- android开发中创建按钮事件监听器的几种方法
- Android两种监听器设置
- Android 四种常用的监听器
- 为Button点击事件注册监听器的两种方法
- 35.注册监听器的两种方法(View.OnClickListener)
- Android按钮监听器实现的四种形式
- android开发中按钮绑定监听器的几种方式
- 给按钮注册监听器的另类方法
- swift创建按钮的两种方法
- JAVA:监控按钮的两种方法
- 跟着别人学ssm+springSecurity(三)
- 写makefiles(二)
- opencv中的图像校正相关
- .NET 采集数据去掉html标签,以逗号分割的
- Thinkphp5+阿里云SMS短信服务发送验证码
- Android 按钮监听器我常用的两种方法
- 第七课 Netty学习之心跳
- Oracle SCN简述
- 期望DP
- 反射
- java异常——捕获异常,并且输出到页面
- java对csv格式的读写操作
- 什么是高内聚,低耦合?
- 淘宝NPM 镜像