其他分享
首页 > 其他分享> > Android:AlertDialog仅在第二次单击任何按钮后关闭

Android:AlertDialog仅在第二次单击任何按钮后关闭

作者:互联网

我有一个带有输入字段和两个按钮(还原,保存)的警报对话框.
当我单击手机上的“后退”按钮时,我要弹出另一个确认对话框,询问:“确定要完成吗?”.所以一切看起来像这样:

public void showNewItemDialog(final int...position) {
    LayoutInflater li = LayoutInflater.from(HostActivity.this);
    View promptsView = li.inflate(R.layout.item_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            HostActivity.this);
    alertDialogBuilder.setView(promptsView);
    userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
    if(position.length>0){
        userInput.setText(listFragment.getmItems().get(position[0]).getTitle());
        userInput.setSelection(userInput.length());
        userInput.requestFocus();
    }
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String title = userInput.getText().toString();
                            if(listFragment.getItemClickType() == Utility.ItemClick.SHORT){
                                listFragment.editRowItem(title, position[0]);
                            }else if(listFragment.getItemClickType() == Utility.ItemClick.LONG){

                            }else if(listFragment.getItemClickType() == Utility.ItemClick.ADD_BUTTON){
                                listFragment.addRowItem(title);
                            }

                        }
                    })
            .setNegativeButton("Revert",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    newItemalertDialog = alertDialogBuilder.create();

    newItemalertDialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                             KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                new AlertDialog.Builder(HostActivity.this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle("Add Item")
                        .setMessage("Are you sure you want to finish?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                newItemalertDialog.dismiss();
                            }
                        }).setNegativeButton("No", null).show();
            }
            return false;
        }
    });
    newItemalertDialog.show();
}

一切正常,但是仅当我在任意按钮上轻按两次(否,是)后,第二个确认对话框才会关闭.
我似乎找不到原因.
谢谢.

解决方法:

OnKey方法被调用了两次:第一次是按下按键,第二次是按下按键,因此您必须过滤:

所以更改如下代码

newItemalertDialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                             KeyEvent event) {
            if (event.getAction() != KeyEvent.ACTION_DOWN)
                return true;

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                      new AlertDialog.Builder(MemberShipActivity.this)
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Add Item")
                            .setMessage("Are you sure you want to finish?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    newItemalertDialog.dismiss();
                                }
                            }).setNegativeButton("No", null).show();
                    Log.e("Key","back");
                }
            return false;
        }
    });

标签:android-alertdialog,alertdialog,android,multiple-alert-dialogs
来源: https://codeday.me/bug/20191118/2025557.html