我是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();
谢谢, 天使
答案 0 :(得分:36)
这可能不是直接的答案。但只是相关主题的一些信息。来自谷歌自己论坛的this帖子,罗曼家伙说..
继续前进,正/负按钮的顺序将是一个 用于ICS。
并且每个操作系统版本的约定是
如果这是一个惯例,那个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中的leftSpacer
,LinearLayout
。 / 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
中的按钮。
因此,首先我们需要移除该空间并使其具有可见性GONE
(invisible
会让它仍占用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
按钮设为否定。