AlertDialog 否定按钮默认高亮显示

AlertDialog negative button by default highlighted

我正在使用带有两个 buttons.Issue 的警报对话框,这里是每当我的警报对话框显示时,它的否定按钮就会显示 highlighted.And 这发生在这个对话框中,只有其他人正在工作 fine.Please 提出一些解决方案。

代码如下:


AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle("Stop On The Go!");
            alert.setMessage(getResources().getString(R.string.tx_confirm_msg_journey));
            alert.setPositiveButton("Stop",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            stopTask();
                        }
                    });
            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            navigateToHomePage();
                            dialog.cancel();
                        }
                    });

            alert.show();

截图:

只关注正面

将此行添加到您的肯定按钮中

 alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){

        @Override
        public void onShow(DialogInterface dialog) {

            Button positive= alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setFocusable(true);
            positive.setFocusableInTouchMode(true);
            positive.requestFocus();
        }
    });

下面代码只改一行。 alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();

 AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());

    alert.setTitle("Stop On The Go!");
    alert.setMessage(getResources().getString(R.string.tx_confirm_msg_journey));
    alert.setPositiveButton("Stop",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    stopTask();
                }
            });
    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    navigateToHomePage();
                    dialog.cancel();
                }
            });

    alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();

您可以按如下方式进行:

    AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));

customBuilder.setTitle(R.string.popup_error_title); 
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {   
                public void onClick(DialogInterface dialog, int which) {  
                    MyActivity.this.finish(); 
                }   
        }); 
AlertDialog dialog = customBuilder.create();
dialog.show(); 

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null) 
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());// set title
alertDialogBuilder.setTitle("TITLE");
    // set dialog message
    alertDialogBuilder
    .setMessage("MESSAGE")
    .setCancelable(false)
    .setPositiveButton("YES",new DialogInterface.OnClickListener(){
    //your positive message
    }
    });
    alertDialogBuilder     //   .setMessage("title")
    .setCancelable(false)
    .setNegativeButton("NO",new DialogInterface.OnClickListener(){
       public void onClick(DialogInterface dialog,int id){
    // if this button is clicked, close
    // current activity
    dialog.cancel();

    }
    });

    // create alert dialog
    AlertDialog alertDialog=alertDialogBuilder.create();

    // show it
    alertDialog.show();
    }

接受的答案没有解决问题。它只是将焦点移动到其他按钮。而 OP

"...dont want focus on any of them."

真正的解决办法是:

alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){

    @Override
    public void onShow(DialogInterface dialog) {//To remove focus from button
        View rootView = ((AlertDialog) dialog).getWindow().getDecorView().findViewById(android.R.id.content);
        rootView.setFocusable(true);
        rootView.setFocusableInTouchMode(true);
        rootView.requestFocus();
    }
});

18 年 7 月 13 日更新: 对于 API 26 是不可接受的。

对于 Kotlin(在 Android 中)

       AlertDialog.Builder(this)
            .setTitle("Title")
            .setMessage("Do you want to cancel the Transaction?")
            .setCancelable(false)
            .setPositiveButton(
                "Yes"
            ) { dialog: DialogInterface, which: Int ->
                dialog.dismiss()
                // for sending data to previous activity use
                // setResult(response code, data)
                finish()
            }
            .setNegativeButton(
                "No"
            ) { dialog: DialogInterface, which: Int -> dialog.dismiss() }
            .show()
            .getButton(AlertDialog.BUTTON_NEGATIVE)
            .requestFocus()