java回调函数实现

12 篇文章 0 订阅
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();
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值