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();
}
本文介绍了一种在Android应用中实现对话框回调功能的方法。通过定义接口和监听器,可以在用户点击确定按钮后触发特定操作,例如关闭当前Activity。文章详细展示了如何创建自定义对话框并设置回调函数。
750

被折叠的 条评论
为什么被折叠?



