Android AlertDialog向左移动PositiveButton,在左侧移动NegativeButton

时间:2012-11-30 11:32:44

标签: android android-alertdialog

我是android的新手。

目前,我想显示一个AlertDialog框,其中包含'OK'和& '取消'按钮。

默认为PositiveButton:Left,NegativeButton:Right

您能告诉我如何将PositiveButton移到右侧& NegativeButton在左侧?

如果我们将文字“OK”更改为NegativeButton&,如果Negativebutton在按OK时会导致声音不好,是否有任何机会/麻烦? “取消”给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();

谢谢, 天使

7 个答案:

答案 0 :(得分:36)

这可能不是直接的答案。但只是相关主题的一些信息。来自谷歌自己论坛的this帖子,罗曼家伙说..

  

继续前进,正/负按钮的顺序将是一个   用于ICS。

并且每个操作系统版本的约定是

  • 在Honeycomb之前的设备上,按钮顺序(从左到右)是 积极 - 中立 - 消极。
  • 在使用Holo主题的新设备上,按钮顺序(左侧为 右)现在是负面的 - 中性 - 正面。

如果这是一个惯例,那个android / Google想要关注,那么你是不是更好,因为你的用户不会单独使用你的应用程序。毕竟用户友好是开发人员寻找的第一件事。

答案 1 :(得分:3)

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

但我建议您遵循惯例,除非您有充分理由更改订单。这将使用户更容易使用您的应用程序。

答案 2 :(得分:0)

无法更改android中的默认设置 但你可以将文本改为cancle 根据这个

设置功能
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();

答案 3 :(得分:0)

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

答案 4 :(得分:0)

AlertDialog的按钮移到右侧的一种非常简单的方法是隐藏处理默认布局的核心XML中的leftSpacerLinearLayout。 / p>

// 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 :(得分:0)

我已经发现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);
        }
 });

然后剩下的就是你的设计,希望有所帮助

答案 6 :(得分:-1)

这不是最优雅的方式,但它会做你想做的事情

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按钮设为否定。