http://blog.csdn.net/wangjinyu501/article/details/22052187

  在Android中到处可见接口回调机制,尤其是UI事件处理方面。举一个最常见的例子button点击事件,button有一个点击方法onClick(),我们知道onclick()是一个回调方法,当用户点击button就执行这个方法。在源码中是这样定义的:
    //这个是View的一个回调接口
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
void onClick(View v);
}

下面看一个简单的例子:

    import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1); button.setOnClickListener(this);
} @Override
public void onClick(View v) {
Toast.makeText(getApplication(), "OnClick", Toast.LENGTH_LONG).show();
} }
这就是一个很典型的例子,当然也可以这样写:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class SSSS extends Activity { private Button button;
private OnClickListener clickListener = new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub }
}; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(clickListener);
} }

下面是View类的setOnClickListener方法,把和回调相关代码贴出来。什么贴它呢,因为Button继承于TextView,而TextView继承于View,在View里面处理的回调:

    /**
*
*/
public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
/**
* Listener used to dispatch click events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnClickListener mOnClickListener; /**
*
* Register a callback to be invoked when this view is clicked. If this view is not
* clickable, it becomes clickable.
*
* @param l The callback that will run
*
* @see #setClickable(boolean)
*/ public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
mOnClickListener = l;
} /**
* Call this view's OnClickListener, if it is defined.
*
* @return True there was an assigned OnClickListener that was called, false
* otherwise is returned.
*/
public boolean performClick() {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); if (mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK); mOnClickListener.onClick(this);
return true;
} return false;
}
}
那现在一起来总结一下基本的回调是如何实现的,首先创建一个接口,这个接口用于你在某 个情景下执行相应的操作。接着创建一个功能类,比如这个类可以显示一个对话框、可以滑动菜单、可以下载数据等等。然后,在这个类里面声明回调接口的对象, 之后在这个类里面创建在某个情景下需要执行的方法,而且在这个方法里面为声明的接口对象赋值。最后在其他的类中使用这个功能类就可以了。所以说,最少也是 需要三个类共同来完成这个回调机制。
    这下大家应该就比较明白了,那我们就自己按照这个方式和流程完成一个这样的例子。以Dialog为例,一般我们在开发时候,经常会用到Dialog。比如一个弹出框,里面有确认和取消。通常情况下,我们可能会这样写:
    final Dialog dialog = new Dialog(this, R.style.MyDialogStyle);
dialog.setContentView(R.layout.dialog_exit_train);
dialog.show();
ImageButton ib_affirm = (ImageButton) dialog
.findViewById(R.id.dialog_exit_ib_affirm);
ImageButton ib_cancel = (ImageButton) dialog
.findViewById(R.id.dialog_exit_ib_cancel); ib_affirm.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { saveUserData();
dialog.dismiss();
TestActivity.this.finish();
}
}); ib_cancel.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { dialog.dismiss();
}
});
也就是得到点击对象之后再去调用onClick(),这样有一个缺点就是你每次都要写,不利于重复使用。那我们就可以对此进行一个封装,看代码:
    import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView; import com.fanfou.app.opensource.R; /**
*
*
*/
public class AlertInfoDialog extends Dialog implements View.OnClickListener {
//创建接口
public static interface OnOKClickListener {
public void onOKClick();
} private final Context mContext;
private TextView mTitleView;
private TextView mTextView; private Button mButtonOk;
private CharSequence mTitle; private CharSequence mText;
//生命接口对象
private OnOKClickListener mClickListener; public AlertInfoDialog(final Context context, final String title,
final String text) {
super(context, R.style.Dialog);
this.mContext = context;
this.mTitle = title;
this.mText = text;
} private void init() {
setContentView(R.layout.dialog_alert); this.mTitleView = (TextView) findViewById(R.id.title);
final TextPaint tp = this.mTitleView.getPaint();
tp.setFakeBoldText(true);
this.mTitleView.setText(this.mTitle); this.mTextView = (TextView) findViewById(R.id.text);
this.mTextView.setText(this.mText); this.mButtonOk = (Button) findViewById(R.id.button_ok);
this.mButtonOk.setOnClickListener(this); } @Override
public void onClick(final View v) {
final int id = v.getId();
switch (id) {
case R.id.button_ok:
cancel();//调用
if (this.mClickListener != null) {
this.mClickListener.onOKClick();
}
break;
default:
break;
}
} @Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBlurEffect();
init();
} protected void setBlurEffect() {
final Window window = getWindow();
final WindowManager.LayoutParams lp = window.getAttributes();
// lp.alpha=0.8f;
lp.dimAmount = 0.6f;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
} public void setMessage(final CharSequence message) {
this.mText = message;
this.mTextView.setText(this.mText);
} public void setMessage(final int resId) {
this.mText = this.mContext.getResources().getText(resId);
this.mTextView.setText(this.mText);
}
//设置监听器 也就是实例化接口
public void setOnClickListener(final OnOKClickListener clickListener) {
this.mClickListener = clickListener;
} @Override
public void setTitle(final CharSequence title) {
this.mTitle = title;
this.mTitleView.setText(this.mTitle);
} @Override
public void setTitle(final int resId) {
this.mTitle = this.mContext.getResources().getText(resId);
this.mTitleView.setText(this.mTitle);
} }

方式和上面介绍的一样,感兴趣的朋友可以自己去实现其他效果的。

Android 中的接口回调的更多相关文章

  1. Android中的接口回调技术

    Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...

  2. JAVA回调函数ANDROID中典型的回调地方

    在计算机中回调函数是指通过函数参数传递到其他代码类的,某一块可执行代码的引用,这以设计允许了底层代码调用者在高层定义的子程序. 在JAVA里面我们使用接口的方式来实现函数的回调. 回调的通俗就是:程序 ...

  3. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  4. java中的接口回调

    [接口回调]接口回调是多态的另一种体现.接口回调是指:可以把使用某一个接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法.当接口变量调用被类实现的接口中 ...

  5. Android中定义接口的方法

    1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...

  6. Android中Parcelable接口

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  7. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  8. Android中Parcelable接口用法

    from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...

  9. Android中Parcelable接口的使用

    在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...

随机推荐

  1. dataList中实现用复选框一次删除多行问题

    先遍历每一行,判断checkBox是否选中,再获取选中行的主键Id 删除就行了 ,,,foreach(DatalistRow rowview in Datalist.Rows) //遍历Datalis ...

  2. 个性化定制——物流app

    众所周知,在互联网不断迈进的大环境下,各行各业都不免在这大潮下纷纷卷入.人们早已不再满足于传统行业,即便是所谓的新兴行业所带来的体验,他们更多的希望能够在便捷的基础上获取更加个性化的服务,个性化服务在 ...

  3. poj1284:欧拉函数+原根

    何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...

  4. 专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现

    专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现 专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现

  5. EL表达式 functions String处理函数

    01.uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  02.上面的 uri 根据 ...

  6. js笔记01

    js编写页面特效动态脚本类型的语言变量:存储数据(日常生活中的东西,比如电视,手机,电脑,出生年份...)语法: var obj=value; obj不能为数字开头,且区分大小写 value对应数据类 ...

  7. Oracle的sql语句中case关键字的用法 & 单双引号的使用

    关于sql中单引号和双引号的使用,来一点说明: 1. 查询列的别名如果含有汉字或者特殊字符(如以'_'开头),需要用双引号引起来.而且只能用双引号,单引号是不可以的. 2. 如果想让某列返回固定的值, ...

  8. C/C++经典面试题目

    1.关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.全局变量,static变量. (2)在栈上创建:在执行函数 ...

  9. 利用dedecms给近三天(或当天)发布的文章显示红色日期或加上new字或new小图片

    1)红色日期 <br>[field:pubdate runphp='yes'] <br>$a="<font color=red>".strfti ...

  10. 1042. Shuffling Machine (20) - sstream实现数字转字符串

    题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...