4

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

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

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

4

2 回答 2

4

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;
        }
    });
于 2016-10-07T10:37:22.417 回答
0
@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方法时将调用。所以你可以在那里添加提示。

于 2016-10-06T04:57:54.230 回答