我想用的按钮,看起来像这样创建一个AlertDialog:https://developer.android.com/images/ui/dialogs_regions.pngAndroid - 如何更改AlertDialog中按钮的外观?
但是,每当我创建了一个AlertDialog.Builder AlertDialog,我最终得到的按钮,看起来像这样:https://developer.android.com/images/ui/dialog_buttons.png
哪有我改变AlertDialog,使按钮看起来像第一个例子(即占用整个对话框的底部,每个按钮之间有灰色的分隔线)?请注意,我并不试图改变对话框窗口的颜色,只是按钮出现在窗口上。理想情况下,我希望只使用默认的Android样式并且不定义自定义样式,这是可能的吗?
下面是我用它来创建和显示AlertDialog代码:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Include answers in summary?");
alertDialogBuilder.setMessage("You have completed " + String.valueOf(questionsCompleted) + " out of 18 questions. Would you like the summary to include these answers along with the questions?");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Include", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
intent.putExtra("componentNumber", 0);
intent.putExtra("includeAnswers", true);
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("Omit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
intent.putExtra("componentNumber", 0);
intent.putExtra("includeAnswers", false);
startActivity(intent);
}
});
alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
有这AlertDialog三个动作。在不太了解细节的情况下,其中两个按钮可让用户参与统计问卷的相同活动,但无论是否包含答案,其他按钮都会取消对话。我知道我应该使用NegativeButton来取消对话框,但即使Android开发人员指南显示对话框中的正面和负面按钮之间会出现一个中性按钮,但我已经获得了Neutral-> Negative- >正。因此,我一直在使用左侧的省略选项和右侧的取消和包含选项进行对话,这对我来说是非常不直观的。
我想将按钮的顺序改为Negative-> Neutral-> Positive,并将负面按钮定义为取消对话框,以便将导致摘要活动的两个按钮组合在一起 - 这是在所有可能的?
为什么不更换代码正确的顺序?首先将您想要的方法放入先出现的按钮并设置相关的按钮文本。用户不知道它是否为中性,正面或负面的按钮。 – Opiatefuchs
,如果您想要一个符合您需要的对话框,只需创建一个自定义对话框。你可以像做活动一样进行布局... https://www.mkyong.com/android/android-custom-dialog-example/ – Opiatefuchs
我试着重新排列代码中的按钮分配,但它没有效果。我担心使用中性按钮进行取消操作的原因是,我担心不同的手机可能会显示不同的按钮,因此用户在实际尝试取消对话时可能会输入一个活动。不过,我想用一个自定义对话框可以解决这个问题,所以我会给它一个镜头 - 感谢教程链接。 –