Android的AlertDialog移动PositiveButton到左边的右边和Negative

2019-07-04 07:42发布

我是新的Android。

目前,我想说明的AlertDialog用“OK”和“取消”按钮,对话框。

默认值是PositiveButton:左,NegativeButton:右

你可以让我知道我可以移动PositiveButton到右侧和NegativeButton到左侧?

有没有如果Negativebutton造成不好的声音按下OK的时候,如果我们改变文本“OK”来NegativeButton和“取消” PositiveButton任何机会/麻烦。

我的代码:

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

谢谢,天使

Answer 1:

这可能不是一个直接的答案。 但是,仅仅对相关话题的一些信息。 从这个线程在谷歌自己的论坛,罗曼盖伊说..

前进的正/负键的顺序将在ICS中使用的一个。

每OS版本的约定

  • 到蜂窝现有装置中,按钮顺序(从左到右)阳性 - 中立 - NEGATIVE。
  • 关于使用全息主题新设备,按钮顺序(从左至右)现负 - 中性 - 正。

如果它是一个惯例,即机器人/谷歌希望效仿,是不是更好,你遵循相同的,因为你的用户不会被单独使用你的应用程序。 所有的用户友好性后的第一件事是开发商寻找..



Answer 2:

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

但我建议,除非你有一个很好的理由来改变为了与公约一起去。 这将使用户更容易使用你的应用程序。



Answer 3:

有没有办法改变在Android中diffault设置,但你可以改变确定CANCLE设置功能accroding这个文本

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


Answer 4:

检查这个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);


Answer 5:

一个非常简单的方式向的按钮转换AlertDialog右手边是隐藏leftSpacer ,一个LinearLayout的核心XML它处理的默认布局中。

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


Answer 6:

我已想出存在的之间的空间布局neutral button-ve/+ve buttons与在的地方为“1” buttonBarLayout其中的按钮。

所以,起初我们需要删除空间,或使它的知名度GONEinvisible会让它仍然需要一个空间,在buttonBarLayout )也是我们最好使用方法onShowListner更好,这样做是显示在对话框后:

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

那么剩下的就是你的设计,希望帮助



Answer 7:

这还不是最优雅的方式,但它会做你想要什么

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

只是使Cancel按钮为正, Ok键为负。



文章来源: Android AlertDialog Move PositiveButton to the right and NegativeButton on the left