1.在接口中定义回调函数
public class AlertOkCancelDialog extends InfoAlertDialog {
//定义一个接口对象
public IAlertOkCancelDialog ialertokcanceldialog;
//定义一个接口
public interface IAlertOkCancelDialog {
public void ok();
}
//定义一个监听方法
public void setOKClickListener(IAlertOkCancelDialog ialertokcanceldialog) {
this.ialertokcanceldialog = ialertokcanceldialog;
}
public AlertOkCancelDialog(Context context, int theme) {
super(context, theme);
initContentView(context);
}
private void initContentView(final Context context) {
View v = View.inflate(context, R.layout.customdialog_ok_cancel, null);
setContentView(v);
v.findViewById(R.id.dialog_btn_ok).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//判断接口对象是否存在,存在则调用接口中的方法
if (null != ialertokcanceldialog) {
ialertokcanceldialog.ok();
}
dismiss();
}
});
v.findViewById(R.id.dialog_btn_cancel).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
}
}
1.2.实现回调函数(在另一个需要的类中实现)
private void showAlertDialog() {
AlertOkCancelDialog alertokcanceldialog = new AlertOkCancelDialog(this, R.style.dialog);
alertokcanceldialog.setCustomTitle(getString(R.string.menu_exit_tips));
alertokcanceldialog.setCustomMessage(getString(R.string.menu_exit_tipscontent));
//此处实现回调函数
alertokcanceldialog.setOKClickListener(new IAlertOkCancelDialog() {
public void ok() {
finish();
System.exit(0);
}
});
alertokcanceldialog.show();
}