Android AlertDialog Move PositiveButton to the rig

2019-02-16 08:40发布

I'm new with android.

Currently I want to show an AlertDialog box with 'OK' & 'Cancel' buttons.

The default is PositiveButton: Left, NegativeButton: Right

Can you let me know how can I move the PositiveButton to the right side & NegativeButton to the left side?

Is there any chance/trouble if Negativebutton cause a bad sound when pressing OK, if We change text "OK" to NegativeButton & "Cancel" to PositiveButton.

My Code:

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
                    builder.setMessage("Confirmation?")
                    .setCancelable(false)
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //Todo
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                    })

dialog = builder.create();

Thanks, Angel

7条回答
We Are One
2楼-- · 2019-02-16 09:03
AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
                builder.setMessage("Confirmation?")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                    })
                    .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                            dialog.cancel();
                        }
                    })


                diaglog = builder.create();

But I recommend to go along with the convention unless you have a good reason to change the order. That will make easier for users to use your application.

查看更多
戒情不戒烟
3楼-- · 2019-02-16 09:08

There is no way to change the diffault setting in android But you can change the text ok to cancle set the functionally accroding this

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
    builder.setMessage("Confirmation?")
           .setCancelable(false)
           .setNegativeButton("OK", 
               new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                   })
           .setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                            dialog.cancel();
                        }
                    });


dialog = builder.create();
查看更多
小情绪 Triste *
4楼-- · 2019-02-16 09:10

A really simple way to shift the buttons of the AlertDialog to the right hand side is to hide the leftSpacer, a LinearLayout within the core XML which handles the default layout.

// Fetch the PositiveButton
final Button       lPositiveButton = lDialog.getButton(AlertDialog.BUTTON_POSITIVE);
// Fetch the LinearLayout.
final LinearLayout lParent         = (LinearLayout) lPositiveButton.getParent();
// Ensure the Parent of the Buttons aligns it's contents to the right.
lParent.setGravity(Gravity.RIGHT);
// Hide the LeftSpacer. (Strict dependence on the order of the layout!)
lParent.getChildAt(1).setVisibility(View.GONE);
查看更多
对你真心纯属浪费
5楼-- · 2019-02-16 09:10

I have figured out that there is a space layout between the neutral button and -ve/+ve buttons with the place "1" in the buttonBarLayout in which the buttons.

So, at first we need to remove that space and or make it's visibility GONE (invisible will let it still takes a space in the buttonBarLayout) also we better to use method onShowListner better that doing that after showing the dialog by:

 alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            LinearLayout view = (LinearLayout) neutralButtonOrAnyOtherBtnFromThe3Btns.getParent();
            Space space= (Space) view.getChildAt(1);
        }
 });

then the rest is your design, wish that helps

查看更多
时光不老,我们不散
6楼-- · 2019-02-16 09:11

check this https://github.com/hslls/order-alert-buttons

dependencies {
    compile 'com.github.hslls:order-alert-buttons:1.0.0'
}


AlertDialogParams params = new AlertDialogParams();
params.mTitle = "title";
params.mMessage = "message";
params.mPositiveText = "Ok";
params.mNegativeText = "Cancel";
params.mCancelable = true;
params.mAlign = BUTTON_ALIGN.POSITIVE_BUTTON_LEFT;  // fix button position
params.mClickListener = new AlertDialogClickListener() {
    @Override
    public void onPositiveClicked() {

    }

    @Override
    public void onNegativeClicked() {

    }
};

AlertDialog dialog = AlertDialogBuilder.createAlertDialog(this, params);
查看更多
我只想做你的唯一
7楼-- · 2019-02-16 09:13

This is not the most elegant of ways but it will do what you want

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
            builder.setMessage("Confirmation?")
                .setCancelable(false)
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //TOdo
                        dialog.cancel();
                    }
                })
                .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //TOdo
                    }
                })


            diaglog = builder.create();

Just make the Cancel button as positive and the Ok button as negative.

查看更多
登录 后发表回答