Android 中的接口回调
http://blog.csdn.net/wangjinyu501/article/details/22052187
//这个是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;
}
}
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();
}
});
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 中的接口回调的更多相关文章
- Android中的接口回调技术
Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...
- JAVA回调函数ANDROID中典型的回调地方
在计算机中回调函数是指通过函数参数传递到其他代码类的,某一块可执行代码的引用,这以设计允许了底层代码调用者在高层定义的子程序. 在JAVA里面我们使用接口的方式来实现函数的回调. 回调的通俗就是:程序 ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- java中的接口回调
[接口回调]接口回调是多态的另一种体现.接口回调是指:可以把使用某一个接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法.当接口变量调用被类实现的接口中 ...
- Android中定义接口的方法
1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable接口用法
from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...
- Android中Parcelable接口的使用
在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...
随机推荐
- python高级编程:有用的设计模式3
# -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#访问者:有助于将算法从数据结构中分离出来"&qu ...
- 小米路由器mini如何设置外网访问wan网站的方法
很多的玩友都在小米路由器mini上面搭建了自己的网站,有些朋友还需要设置对外网进行开放,我自己也在路由器上面实践了使用,下面与大家分享一下如何设置外网访问路由器网站的办法. 工具/原料 小米路由器mi ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
- HDU 4760 Good FireWall 完好Trie题解
本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...
- java通过jsp的Excel导出
在项目中一般导出报表用poi,可是假设你不想用框架就用简单的jsp也能够实现报表导出.并且实现起来还特别简单. 先看一下效果截图: 点击导出后的效果截图: 详细实现: 第一:在页面的列表页面中就是普通 ...
- FpSpread添加表头(列名)标注
for (int j = 0; j < fp.ActiveSheetView.ColumnCount; j++) { fp.ActiveSheetView.ColumnHeader.Cells[ ...
- oracle 全文检索
一.使用 sys 用户登录oracle (1)运行—cmd—sqlplus — sys/密码 @连接字符 as sysdba 二.授权 1.grant ctxapp to 全文检索使用用户: 2.gr ...
- iOS_SN_基于AFNetworking3.0网络封装
转发文章,原地址:http://www.henishuo.com/base-on-afnetworking3-0-wrapper/?utm_source=tuicool&utm_medium= ...
- ASP.NET MVC4 + Highcharts生成报表
//后端 public ActionResult TighteningReport(BReportTighteningReportModel model, string rate, string we ...
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...