Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试以编程方式关闭警报对话框_Android_Dialog - Fatal编程技术网

Android 尝试以编程方式关闭警报对话框

Android 尝试以编程方式关闭警报对话框,android,dialog,Android,Dialog,我正在尝试做一些简单的事情,这在很多变体中被多次询问,但在我的案例中我无法做到 我正在尝试这样的场景: 我有一个弹出警报对话框窗口(某些项目),其中有一个删除按钮,如果您按下删除按钮,它将弹出另一个警报对话框“您确定要删除吗”和是/否按钮 如果您按yes(是),它将删除该项,然后我希望两个对话框都关闭 “是/否”对话框是自动关闭的,因为我按下了按钮,但“项目”对话框没有关闭,我无法以任何方式将其关闭 这是我的密码: private void popupItem(final int item_i

我正在尝试做一些简单的事情,这在很多变体中被多次询问,但在我的案例中我无法做到

我正在尝试这样的场景:

我有一个弹出警报对话框窗口(某些项目),其中有一个删除按钮,如果您按下删除按钮,它将弹出另一个警报对话框“您确定要删除吗”和是/否按钮

如果您按yes(是),它将删除该项,然后我希望两个对话框都关闭

“是/否”对话框是自动关闭的,因为我按下了按钮,但“项目”对话框没有关闭,我无法以任何方式将其关闭

这是我的密码:

private void popupItem(final int item_id) {

        //setup popup window
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), android.R.style.Holo_Light_ButtonBar_AlertDialog));

        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View dialoglayout = inflater.inflate(R.layout.template_popup_item_alert, null);

        builder.setView(dialoglayout);
        builder.setTitle(getString(R.string.edit_item));

        final AlertDialog item_alert = builder.create();


        //setup delete button
        final ImageButton deleteImageButton = (ImageButton) dialoglayout.findViewById(R.id.template_popup_item_delete);
        deleteImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickDeleteItem(item_id, item_alert);
            }
        });


        builder.show();

    }
以下是删除功能:

private void clickDeleteItem(final int item_id, final AlertDialog item_alert) {
        // create (another) popup delete for "are you sure? yes/no"
        AlertDialog.Builder alert_yesno = new AlertDialog.Builder(getActivity());

        alert_yesno.setTitle(getString(R.string.want_to_remove));

        alert_yesno.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                // ... deleting the item(id)..

                dialog.dismiss(); // close yes/on dialog (redundant - it is closed by itself anyway)
                item_alert.dismiss(); // close the previous popup dialog //~~ NOT WORKING!

            }
        });

        alert_yesno.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
            }
        });

        alert_yesno.show();
    }
如您所见,我正在将项对话框指针传递到yes/no对话框中,以便它可以关闭它,但该行不起作用

当我到达以下行时,调试器上的一切都正常运行:

item_alert.dismiss();
它正确地指向“项”对话框,但此命令无法关闭它。。 我尝试了
.close()命令,但同样的事情,什么也没发生

我错过了什么? 谢谢你

你在打电话吗

builder.show()

而不是


item_alert.show()

感谢Nishant Paradamwar,以下是正确答案:

public class Popup{
    AlertDialog item_alert; // *creating class object of it*

    private void popupItem(final int item_id) {

            //setup popup window
            final AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(getActivity(), android.R.style.Holo_Light_ButtonBar_AlertDialog));

            LayoutInflater inflater = getActivity().getLayoutInflater();
            final View dialoglayout = inflater.inflate(R.layout.template_popup_item_alert, null);

            builder.setView(dialoglayout);
            builder.setTitle(getString(R.string.edit_item));




            //setup delete button
            final ImageButton deleteImageButton = (ImageButton) dialoglayout.findViewById(R.id.template_popup_item_delete);
            deleteImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickDeleteItem(item_id); // *removed 2nd argument*
                }
            });

            item_alert = builder.create(); // *moved the create to bottom so it will contain all parts*
            item_alert.show(); // *calling the alert not builder*

        }

        private void clickDeleteItem(final int item_id) { // *removed second argument, its in class now*
            // create (another) popup delete for "are you sure? yes/no"
            AlertDialog.Builder alert_yesno = new AlertDialog.Builder(getActivity());

            alert_yesno.setTitle(getString(R.string.want_to_remove));

            alert_yesno.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    // ... deleting the item(id)..

                    item_alert.dismiss(); // close the previous popup dialog //~~ *WORKING NOW!

                }
            });

            alert_yesno.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });

            alert_yesno.show();
        }
}

设置所有标题、setPositiveButton、setNegativeButton和消息,然后调用builder.create();它将返回AlertDialog对象,您可以显示/取消该对象,但请记住,在构建器之后无法操作构建器。create()被称为doh nice,我这次做对了!我需要将答案与您之前创建类对象的答案结合起来。。非常感谢你!!我为此挣扎了几天!