默认情况下,AlertDialog负按钮突出显示

时间:2016-02-25 07:43:35

标签: android android-alertdialog

我正在使用带有两个按钮的警告对话框。这里的问题是,每当我的警告对话框显示时,其显示的负按钮都会突出显示。这是在此对话框中发生的,只有其他工作正常。请提示一些解决方案。

以下是代码:

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

截图: enter image description here

6 个答案:

答案 0 :(得分:6)

显示以下代码只需更改一行。 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();

答案 1 :(得分:3)

只关注积极的

在正面按钮中添加此行

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

答案 2 :(得分:2)

对于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()

答案 3 :(得分:1)

您可以按照以下方式执行此操作:

db

答案 4 :(得分:0)

var home = Home()
@IBOutlet weak var homeTableView: UITableView!

override func viewDidLoad()
{
    super.viewDidLoad()

    self.homeTableView.delegate   = self
    self.homeTableView.dataSource = self
    self.homeTableView.reloadData()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("homeCell", forIndexPath: indexPath) as! HomeCell
    cell.contentLabel.text = home.homeDisplayContent(indexPath.row)
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return self.home.contents.count
}

答案 5 :(得分:0)

接受的答案并没有解决问题。它只是将焦点移动到其他按钮。而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();
    }
});

更新13/07/18: API 26无法接受。