Android编程之Listener侦听的N种写法及实现原理
写下这个题目时突然想起鲁迅笔下的孔乙已,茴香豆的几种写法,颇有些咬文嚼字的味道。虽然从事手机编程多年,但一直使用的是C和C++编程,由于安卓早期只支持JAVA开发,所以对于时下如火如荼的安卓系统,我一直观之而未入之。现在由于工作需要开始研究安卓编程,由于以前主要使用C语言,乍遇JAVA,在思考方式上,写法上,编程规范上所遇问题颇多。单单一个Listener方法,在是否使用匿名类匿名对象时,就是各种不同的写法。OnClickListener和其他Listener方法一样,都是View类的抽象接口,重载后就能使用,定义如下:
// 编译自View.java (版本 1.5:49.0,无超级位)
public abstract static interface android.view.View$OnClickListener {
// 方法描述符 #4 (Landroid/view/View;)V
public abstract void onClick(android.view.View arg0);
内部类:
[内部类信息: #1 android/view/View$OnClickListener, 外部类信息: #7 android/view/View
内部名: #9 OnClickListener, 访问标志:1545 public abstract static]
}
这是一个抽象接口的定义,在使用上可以像类一样派生。抽象接口interface)和抽象类(class)是和C,C++不一样的,但在JAVA中两者比较相似,但却又是不同的,有关这方面的概率可以从JAVA编程中了解到,C++程序员也许会对这两者感觉不知所措,不知道该为某些实现创建抽象接口还是抽象类。这可能需要一定的代码实战经验才能更好的把握。
Listener在使用上有多种写法,了解这些,对编写程序好处比较有限,但对阅读代码却又是有用的。大约也可以像孔乙已一样拿来炫耀吧,但我认为,这对初涉安卓编程的其他程序员来深入了解JAVA或者安卓编程,具有很重要的意义。本例使用了六种方法,由于JAVA语法的灵活性,很可能换种思考,一种新的方法就诞生了,所以本文仅做了解,不要让他成为你的灵魂锁链,导致限制了你在安卓领域做更深入更广泛的探索和贡献。当然如果你发现的新的写法或者创造什么新的写法,也可以告诉我,大家一起学习。下面是程序代码:
使用eclipse创建安卓工程,假设工作名字使用button4,包使用com.mypack,在窗口中加入6个BUTTON.,使用默认命名,系统自动会命名为button1到button6,再加入一个editText,系统会自动命名为editText1.工程项目包名都可以随意。
资源的main.xml文件内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button1" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button2" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button3" />
- <Button
- android:id="@+id/button4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button4"
- android:onClick="Btn4OnClick" />
- <Button
- android:id="@+id/button5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button5" />
- <Button
- android:id="@+id/button6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button6" />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="click button">
- <requestFocus />
- </EditText>
- </LinearLayout>
Button4Activity.java文件内容为:
- package com.mypack;
- import com.mypack.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.*;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import com.mypack.callOut;
- public class Button4Activity extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- private Button m_button1, m_button2, m_button3, m_button4, m_button5,
- m_button6;
- public EditText ET;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- m_button1 = (Button) findViewById(R.id.button1);
- m_button2 = (Button) findViewById(R.id.button2);
- m_button3 = (Button) findViewById(R.id.button3);
- m_button4 = (Button) findViewById(R.id.button4);
- m_button5 = (Button) findViewById(R.id.button5);
- m_button6 = (Button) findViewById(R.id.button6);
- ET = (EditText) findViewById(R.id.editText1);
- /*
- * 方法1,其中的this相当于new OnClickListener()对象, 即class test 中的一个对象,
- * 而如果要用这种方式的话,public void onClick 方法必须写在该test类中, 且在开头使用implements
- * OnClickListener, 即this对象可以直接调用该方法
- */
- m_button1.setOnClickListener(this);
- //方法2,使用对象clickListener
- m_button2.setOnClickListener(clickListener);
- //方法3,使用匿名对象创建监听,同方法论,可以看作另一种写法
- m_button3.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- String strTmp = "点击Button03";
- ET.setText(strTmp);
- }
- });
- //方法4,使用XML文件创建时绑定方法Btn4OnClick
- //方法5,自己设计个监听类,监听的方法引用OnClickListener接口中的方法,创建的是匿名对象
- m_button5.setOnClickListener(new clickListener2());
- //方法6, 外部类实现事件监听器接口,很少用 ,详看文件callout.java
- m_button6.setOnClickListener(new callOut(this));
- }
- @Override
- public void onClick(View v) {
- Log.i("log", "click");
- String strTmp = "点击Button01";
- ET.setText(strTmp);
- }
- public OnClickListener clickListener = new OnClickListener() {
- public void onClick(View v) {
- Log.i("log", "click");
- String strTmp = "点击Button02";
- ET.setText(strTmp);
- }
- };
- public class clickListener2 implements View.OnClickListener {
- public void onClick(View v) {
- Log.i("log", "click");
- String strTmp = "点击Button05";
- ET.setText(strTmp);
- }
- };
- public void Btn4OnClick(View view) {
- String strTmp = "点击Button04";
- ET.setText(strTmp);
- }
- }
文件中最后一个按钮使用了类callOut,CALLOUT.java代码如下:
- package com.mypack;
- import android.app.Activity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import com.mypack.Button4Activity;
- public class callOut implements OnClickListener {
- private Button4Activity act;
- public callOut(Activity activity)
- {
- act = (Button4Activity)activity;
- }
- @Override
- public void onClick(View v)
- {
- String strTmp = "点击Button06";
- act.ET.setText(strTmp);
- }
- }
Android编程之Listener侦听的N种写法及实现原理的更多相关文章
- Android Listener侦听的N种写法
Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种不同的写法. OnClickListener和其他Listener方法一样,都是View类的接口,重载实现后就能使用 ...
- 【转】Android Listener侦听的N种写法
原文网址:http://blog.csdn.net/ithomer/article/details/7489274 Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种 ...
- Android Listener 监听的几种写法
Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种不同的写法. OnClickListener和其他Listener方法一样,都是View类的接口,重载实现后就能使用 ...
- Android编程之LayoutInflater的inflate方法实例
假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...
- Android编程之LayoutInflater的inflate方法详解
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...
- Android开发之bindService()侦听service内部状态
在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 p ...
- Android代码规范----按钮单击事件的四种写法
[前言] 按钮少的时候用第三种的匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类. 按钮多的时候一般选择第四种写法. 一.第一种写法:在XML文件中声明onClick属性(很少用) 在XM ...
- Android开发系列之按钮事件的4种写法
经过前两篇blog的铺垫,我们今天热身一下,做个简单的例子. 目录结构还是引用上篇blog的截图. 具体实现代码: public class MainActivity extends Activity ...
- Android开发系列之button事件的4种写法
经过前两篇blog的铺垫,我们今天热身一下,做个简单的样例. 文件夹结构还是引用上篇blog的截图. 详细实现代码: public class MainActivity extends Activit ...
随机推荐
- firefox浏览器强制取消自动更新
问题:Firefox浏览器,在浏览器的设置中已经设置了取消自动升级,实际退出Firefox浏览器重新启动浏览器后还是会升级到最新版本.影响:Firefox浏览器不同的版本的插件的支持兼容不一样,如果需 ...
- Centos 7安装的一些事项
一.Wifi无法连接 ip addr 显示:unmanaged, plugin missing 先连有线网yum install -y NetworkManager-wifi systemctl re ...
- h5py库
参考文献:http://docs.h5py.org/en/latest/high/dataset.html h5py文件存放数据集(dataset)和组(group). dataset类似数组类的数据 ...
- React全栈-社交网络程序 提交表单数据
1. 给每个input 表格添加change 事件 当input 变化时触发 <div className="form-group"> <input type= ...
- 怎么让小白理解intel处理器(CPU)的分类
https://www.zhihu.com/question/32669957 目录 如何选购台式机CPU? 1. 英特尔处理器简介(本文) 1.1 聊聊Intel Tick-Tock 2. AMD处 ...
- 巴厘岛的雕塑(sculptures)
巴厘岛的雕塑(sculptures) 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 N 座雕塑,为方便起见,我们把这些雕塑从 1 到 N 连续地进行标号,其中第 i ...
- 用 Flask 来写个轻博客 (5) — (M)VC_SQLAlchemy 的 CRUD 详解
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 SQLAlchemy 的 CRUD Create 增添数据 ...
- 模板引擎的简单原理template
var templateStr = "我的名字叫<%=name%>我是一只小狗,今年<%=age%>岁."; var data = { name:'旺财 ...
- JSP 虚拟路径设置
编辑server.xml 在Host标签内加 :path为虚拟路径 docBase为绝对路径 <Context path="/icon" docBase="C ...
- 一个普通函数的冷僻属性(length、caller、arguments、name、[[Scopes]]和[[FunctionLocation]])
https://blog.csdn.net/qq_17175013/article/details/81915059