Android关闭AlertDialog的方法

新手第一帖,大神们见笑了!希望大家补充更好的方法!!
最近写了个AlertDialog dialog对话框,通过android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);显示对话框,结果发现Builder没有dialog.cancel()和dismiss()方法关闭对话框。自己摸索出了一种关闭AlertDialog dialog对话框的方法。
直接上代码

builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("标题");
builder.setCancelable(false);//点击屏幕和返回键对话框不消失
RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.fist, null);
builder.setView(relativeLayout);
builder.setCancelable(false);
Button cancel = (Button) relativeLayout.findViewById(R.id.cancl);
Button ok = (Button) relativeLayout.findViewById(R.id.ok);
builder.create().show();

这样通过created()加show()方法弹出的对话框在使用builder.setCancelable(false)之后,本人尚未找的好的让对话框的方法。只能用dialog的dismiss()方法让对话框消失
代码如下

final Dialog dialog;
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("标题");
builder.setCancelable(false);//点击屏幕和返回键对话框不消失
RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.fist, null);
builder.setView(relativeLayout);
builder.setCancelable(false);
Button cancel = (Button) relativeLayout.findViewById(R.id.cancl);
Button ok = (Button) relativeLayout.findViewById(R.id.ok);
dialog = builder.show();
ok.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    // TODO Auto-generated method stub
        dialog.dismiss();
    }
});
dialog.show();

通过builder.show();可以使用dialog.cancel()和dismiss()方法关闭对话框。
一定不要忘了dialog.show();
当然也可以使用setNegativeButton,setPositiveButton和setNeutralButton,然后在里面添加监听事件用来关闭对话框。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在AlertDialog上调用`show()`方法时,它会返回一个Dialog对象,你可以使用它来监听对话框上的按钮点击事件。 例如: ``` AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("标题") .setMessage("消息") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮时的逻辑 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮时的逻辑 } }) .create(); dialog.show(); ``` 在上面的示例中,我们在AlertDialog上设置了两个按钮:确定和取消。当用户点击其中一个按钮时,会分别触发相应的`onClick()`方法。 因此,你可以在点击按钮时执行所需的操作,然后在`onClick()`方法关闭对话框并返回。例如: ``` AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("标题") .setMessage("消息") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮时的逻辑 // 关闭对话框并返回 dialog.dismiss(); finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮时的逻辑 // 关闭对话框并返回 dialog.dismiss(); finish(); } }) .create(); dialog.show(); ``` 在上面的示例中,我们在点击按钮时关闭对话框并调用了`finish()`方法来返回。当你调用`finish()`方法时,当前Activity会被销毁并返回上一个Activity。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值