How to set focus to android alert dialog negative

2019-02-21 16:43发布

问题:

I have written code to setFocus to ALert Dialog negative button by using requestFocus(). But the button color will not change.I can able to set background image to that button manually.But i need that one directly from native.How to give focus to second button in ALlert Dialog?

Iam sending the code snippet for understanding

alertbox.show();
alertbox.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();

Even I tried with

alertbox.show();
alertbox.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus(View.FOCUS_FORWARD)

Please any one can responde on this query?

Regards, Android Developer

回答1:

Just setOnShowListener() to AlertDialog, and set focus on the negative button.

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){

        @Override
        public void onShow(DialogInterface dialog) {

            Button negative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negative.setFocusable(true);
            negative.setFocusableInTouchMode(true);
            negative.requestFocus();
        }
    });
    alertDialog.show();