AlertDialog.dismiss() 不起作用 [英] AlertDialog.dismiss() not working

查看:207
本文介绍了AlertDialog.dismiss() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用否定和肯定按钮.我需要关闭对话框,但 dialog.dismiss() 没有效果.

I am not using the negative and positive buttons. I need to close the dialog but dialog.dismiss() has no effect.

        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setView(view);
        alert.setCancelable(false);
        dialog = alert.create();
        goButton.setOnClickListener(new View.OnClickListener() { //goButton is inside view which is inflated inside the dialog

            @Override
            public void onClick(View view) {
                age = (String)spinner.getSelectedItem();
                if(gender == null){
                    Utils.makeToast(context, "Select your gender");
                }else if(age == null || age.toLowerCase().contains("age") || age.equals("")){
                    spinner.performClick();
                }else{
                    Utils.makeToast(context, (String)spinner.getSelectedItem() + " - gender: " +gender);
                    editor.putInt("age", Utils.getIntOrZero(age));
                    editor.putString("gender", gender);
                    editor.commit();
                    dialog.dismiss(); // called but not working
                }
            }
        });

        alert.show();

推荐答案

它不会被关闭,因为您正在调用 AlertDialog.dismissAlertDialog 不是显示的相同.换句话说,您正在调用 alert.show() 并使用 dialog.dismiss().要修复它,请调用 dialog.show().

It isn't dismissing because the AlertDialog you're calling AlertDialog.dismiss on isn't the same one that's shown. In other words, you're calling alert.show() and using dialog.dismiss(). To fix it call dialog.show().

这篇关于AlertDialog.dismiss() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆