Android 如何在 AlertDialog 中设置正负按钮的顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11459827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set the order of the positive and negative buttons in AlertDialog?
提问by Micah Hainline
Why I want to do this is another discussion entirely, but I need to figure out the best way to make all my alert dialogs have the positive button on the right side. Note that in version 3.0 and below the buttons normally appear as OK / Cancel and in 4.0 and above it is Cancel / OK. I want to force my application to use Cancel / OK in the simplest way possible. I have a lot of AlertDialogs in the application.
为什么我要这样做完全是另一个讨论,但我需要找出使我的所有警报对话框都在右侧具有肯定按钮的最佳方法。请注意,在 3.0 及以下版本中,按钮通常显示为 OK / Cancel,而在 4.0 及以上版本中,则显示为 Cancel / OK。我想强制我的应用程序以最简单的方式使用取消/确定。我在应用程序中有很多 AlertDialogs。
采纳答案by thegrinner
Unfortunately, I don't believe you can. However, to quote the documentation:
不幸的是,我不相信你可以。但是,引用文档:
Note: You can only add one of each button type to the AlertDialog. That is, you cannot have more than one "positive" button. This limits the number of possible buttons to three: positive, neutral, and negative. These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what.
注意:您只能将每种按钮类型中的一种添加到 AlertDialog。也就是说,您不能有多个“肯定”按钮。这将可能的按钮数量限制为三个:正面、中性和负面。这些名称在技术上与按钮的实际功能无关,但可以帮助您跟踪哪个按钮的作用。
So you can turn the different buttons into whatever you want. What you're seeing here is the order having switched (ordering from thisanswer):
所以你可以把不同的按钮变成你想要的任何东西。您在这里看到的是已切换的顺序(从这个答案中排序):
- On devices prior to ICS, the button order (left to right) was POSITIVE - NEUTRAL - NEGATIVE.
- On newer devices using ICS, the button order (left to right) is now NEGATIVE - NEUTRAL - POSITIVE.
- 在 ICS 之前的设备上,按钮顺序(从左到右)是正 - 中性 - 负。
- 在使用 ICS 的较新设备上,按钮顺序(从左到右)现在是负 - 中性 - 正。
You might try checking the Build.VERSION
and using that to decide which button is which at runtime.
您可以尝试检查Build.VERSION
并使用它来决定运行时哪个按钮是哪个。
回答by u1584100
this is my solution. It is work for me.
这是我的解决方案。这对我来说是工作。
// Show alertDialog after building
AlertDialog alertDialog = createAlertDialog(context);
alertDialog.show();
// and find positiveButton and negativeButton
Button positiveButton = (Button) alertDialog.findViewById(android.R.id.button1);
Button negativeButton = (Button) alertDialog.findViewById(android.R.id.button2);
// then get their parent ViewGroup
ViewGroup buttonPanelContainer = (ViewGroup) positiveButton.getParent();
int positiveButtonIndex = buttonPanelContainer.indexOfChild(positiveButton);
int negativeButtonIndex = buttonPanelContainer.indexOfChild(negativeButton);
if (positiveButtonIndex < negativeButtonIndex) {
// prepare exchange their index in ViewGroup
buttonPanelContainer.removeView(positiveButton);
buttonPanelContainer.removeView(negativeButton);
buttonPanelContainer.addView(negativeButton, positiveButtonIndex);
buttonPanelContainer.addView(positiveButton, negativeButtonIndex);
}
回答by CelinHC
You can extend the AlertDialog.Builder
to force the order:
您可以扩展AlertDialog.Builder
以强制顺序:
public class MyDialog extends AlertDialog.Builder {
public MyDialog(Context arg0) {
super(arg0);
}
@Override
public Builder setPositiveButton(CharSequence text, OnClickListener listener) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return super.setNegativeButton(text, listener);
} else {
return super.setPositiveButton(text, listener);
}
}
@Override
public Builder setNegativeButton(CharSequence text, OnClickListener listener) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return super.setPositiveButton(text, listener);
} else {
return super.setNegativeButton(text, listener);
}
}
}
回答by rude
I've created a solution to force order of buttons for any Android version on any device.
我已经创建了一个解决方案来强制任何设备上任何 Android 版本的按钮顺序。
The idea is that we can get list of buttons of dialog in order they appear on screen and set text and actions in order we want. It guarantees that we would have same order on any device.
这个想法是我们可以获得对话框按钮列表,以便它们出现在屏幕上,并按照我们想要的顺序设置文本和操作。它保证我们在任何设备上都有相同的顺序。
Please check my GitHub repository
请检查我的GitHub 存储库
Here is example of creating and showing dialog:
这是创建和显示对话框的示例:
new FixedOrderDialogFragment.Builder()
.setLeftButtonText(R.string.left)
.setCenterButtonText(R.string.center)
.setRightButtonText(R.string.right)
.setDefaultButton(FixedOrderDialogFragment.BUTTON_RIGHT)
.setMessage(R.string.message)
.setTitle(R.string.app_name)
.create()
.show(getSupportFragmentManager(), "DEMO");
Please note that owner Activity should implement FixedOrderDialogFragment.FixedOrderDialogListener to be able restore dialog state on Activity re-creation.
请注意,所有者 Activity 应实现 FixedOrderDialogFragment.FixedOrderDialogListener 才能在 Activity 重新创建时恢复对话框状态。
回答by Dave S
I figured I could just set the text for the positive button to Cancel and the negative to OK but it turns out they are in alphabetical order.
我想我可以将正按钮的文本设置为取消,将负按钮的文本设置为确定,但事实证明它们是按字母顺序排列的。
回答by willkernel
- In AlertDialog buttonPanel,the three buttons sort by [Neutral] [Negative] [Positive]
then you can set buttons like following codes
builder.setNeutralButton("Negative",listener);
builder.setNegativeButton("Neutral",listener);
builder.setPositiveButton("Positive",listener);
- 在 AlertDialog buttonPanel 中,三个按钮按 [Neutral] [Negative] [Positive] 排序
然后你可以设置按钮如下代码
builder.setNeutralButton("Negative",listener);
builder.setNegativeButton("Neutral",listener);
builder.setPositiveButton("Positive",listener);
回答by Jan Bergstr?m
Sometimes completely different perspectives are the solution to a problem, the HI-user culture.
有时完全不同的观点是解决问题的方法,即 HI 用户文化。
- In Win32 an OK and a Cancel button is absolutely necessary or the dialog will not work.
In Android the HI is quite different because of the existence of the OS "Back" button(onBackPressed() that could also be overridden, ESC on attached physical keyboards) in the operative system HI.
- There is no need for a Negative/cancel button in most cases.
- So most dialog are enough with the Positive button
- Then there are no button order issue, the positive button is alone?
- If you are in an edit field set it to edittext.setSingleLine(); and enter will move focus to the single positive button. Enter again and its positive button pressed.
- If the user wants to push Cancel, he presses the OS "Back" button(As a developer we do not need to explain the core of the OS HI policy to the user, that is an issue of the OS issuer. We just leave the Android dialog without a Cancel button.)
- 在 Win32 中,确定和取消按钮是绝对必要的,否则对话框将无法工作。
在 Android 中,HI 完全不同,因为操作系统 HI 中存在操作系统“后退”按钮(onBackPressed() 也可以被覆盖,连接物理键盘上的 ESC)。
- 大多数情况下不需要否定/取消按钮。
- 所以大多数对话都足够使用正按钮
- 那么就没有按键顺序问题,只有正按键吗?
- 如果您在编辑字段中,请将其设置为 edittext.setSingleLine(); 并输入将焦点移动到单个正按钮。再次输入并按下它的正按钮。
- 如果用户想按取消,他按下操作系统的“返回”按钮(作为开发者,我们不需要向用户解释操作系统 HI 策略的核心,这是操作系统发行者的问题。我们只留下没有取消按钮的 Android 对话框。)