Android 默认情况下,AlertDialog负按钮高亮显示

Android 默认情况下,AlertDialog负按钮高亮显示,android,android-alertdialog,Android,Android Alertdialog,我正在使用带有两个按钮的警报对话框。问题是,每当我的警报对话框显示时,它的负按钮就会突出显示。在这个对话框中发生这种情况,只有其他按钮工作正常。请建议一些解决方案 代码如下: 截图: 只需关注积极的一面 将这一行添加到您的肯定按钮中 alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){ @Override public void onShow(DialogInterface d

我正在使用带有两个按钮的警报对话框。问题是,每当我的警报对话框显示时,它的负按钮就会突出显示。在这个对话框中发生这种情况,只有其他按钮工作正常。请建议一些解决方案

代码如下:


截图:

只需关注积极的一面

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

 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_正值).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));

公认的答案并不能解决问题。它只是将焦点移动到另一个按钮。鉴于OP

“……不要把注意力集中在其中任何一个上。”

真正的解决办法是:

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();
    }
});
2018年7月13日更新:不适用于API 26。

适用于Kotlin(Android系统)


至少更改:
单击“停止”开始…
到对话框消息中的
单击“停止”开始…
。@Strider thanx可以,建议一些solution@Swapnil你试过我的答案了吗?你想关注其他按钮吗?@sanky jain不,我不想关注任何一个按钮。我只是想要一个简单的警报对话框。像这样更改dailog..并让我知道它是否有效:)@Swapnil很高兴帮助快乐编码:)不工作。requestFocus()尝试
alert.show().getButton(DialogInterface.button\u否定).clearFocus()
如果是complite,则编辑answer@Swapnil应用程序使用哪个主题
    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();
    }
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();
    }
});
       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()