//设置监听器 也就是实例化接口
public void setOnClickListener(final OnOKClickListener clickListener) {
this.mClickListener = clickListener;
}
然后在需要的回调的地方实现方法
if (this.mClickListener != null) {
this.mClickListener.onOKClick();
}
例子:
package zn.zj.jy.myview.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import zn.zj.jy.R;
/**
* <p>
* Title: CustomDialog
* </p>
* <p>
* Description:自定义Dialog(参数传入Dialog样式文件,Dialog布局文件)
* </p>
* <p>
* Copyright: Copyright (c) 2014
* </p>
*
* @author jgduan
* @version 1.02
*/
public class LoginDialog extends Dialog implements
View.OnClickListener {
//创建接口
public static interface OnOKClickListener {
public void onOKClick();
}
//生命接口对象
private OnOKClickListener mClickListener;
/** 布局文件 **/
int layoutRes;
/** 上下文对象 **/
Context context;
/** 确定按钮 **/
private Button confirmBtn;
/** Toast时间 **/
public static final int TOAST_TIME = 1000;
public LoginDialog(Context context) {
super(context);
this.context = context;
}
/**
* 自定义布局的构造方法
*
* @param context
* @param resLayout
*/
/**
* 自定义主题及布局的构造方法
*
* @param context
* @param theme
* @param resLayout
*/
public LoginDialog(Context context, int theme, int resLayout) {
super(context, theme);
this.context = context;
this.layoutRes = resLayout;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 指定布局
this.setContentView(layoutRes);
// 根据id在布局中找到控件对象
confirmBtn = (Button) findViewById(R.id.confirm_btn);
// 设置按钮的文本颜色
confirmBtn.setTextColor(0xff1E90FF);
// 为按钮绑定点击事件监听器
confirmBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.confirm_btn:
// 点击了确认按钮
dismiss();
if (this.mClickListener != null) {
this.mClickListener.onOKClick();
}
break;
default:
break;
}
}
//设置监听器 也就是实例化接口
public void setOnClickListener(final OnOKClickListener clickListener) {
this.mClickListener = clickListener;
}
}