【问题标题】:Android: AlertDialog closes only after second click on any buttonAndroid:AlertDialog仅在第二次单击任何按钮后关闭
【发布时间】:2016-10-06 04:37:20
【问题描述】:

我有一个带有输入字段和两个按钮(恢复、保存)的警报对话框。 当我点击手机上的“返回”按钮时,我希望弹出另一个确认对话框,询问:“你确定要完成吗?”。所以这一切看起来像这样:

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();
}

一切正常,但第二个确认对话框只有在我点击任何按钮两次(否,是)后才会关闭。 我似乎无法找到造成这种情况的原因。 谢谢。

【问题讨论】:

    标签: android android-alertdialog multiple-alert-dialogs


    【解决方案1】:

    OnKey方法被调用了两次:第一次是key down,第二次key up,所以要过滤:

    所以改成下面的代码

    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;
            }
        });
    

    【讨论】:

    • 完美!谢谢。
    【解决方案2】:
    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        MyActivity.this.finish();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();
    
    }
    

    试试这个代码。 当你按回 OnBackPressed 方法将调用。所以你可以在那里添加提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2014-02-08
      相关资源
      最近更新 更多