如何制作AlertDialog自定义按钮android?
How to make AlertDialog custom buttons android?
我有一个警告对话框,目前看起来是这样的:
Current Alert Dialog
但我希望 "YES" 和 "NO" 按钮有背景,或者看起来像按钮而不只是文本。我已经尝试为 AlertDialog 实现自定义主题,但我能得到的唯一更改是更改 "WARNING" 文本颜色。
自定义警报对话框的主题以便用实际按钮代替 "YES" 和 "NO" 的最有效和最简单的方法是什么?
这是我当前的 res/values/styles.xml 文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorAccent">@color/PCCA_blue</item>
</style>
<style name="customAlertDialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:textColor">@color/red</item>
</style>
这是我构建和显示 AlertDialog 的地方:
private void alert(final String action){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customAlertDialog));
String message = "";
switch(action) {
case "upload":
message = "Are you sure you want to upload the counted quantities?\n\nIt may take a few minutes to reflect your changes.";
break;
case "download":
message = "Downloading new worksheets may overwrite existing counted quantities, do you want to continue?\n\n" +
"NOTE: You may want to upload counts first.\n\n" +
"(Changed counted quantities will still be available for upload)";
break;
default:
message = action;
break;
}
alertDialogBuilder.setTitle("WARNING");
alertDialogBuilder.setMessage(message);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE) {
if (action.equals("upload")) {
MainActivity.upload(context);
} else if (action.equals("download")) {
MainActivity.download(context);
}
} else {
dialog.cancel();
}
}
};
alertDialogBuilder.setPositiveButton("YES", listener);
alertDialogBuilder.setNegativeButton("NO", listener);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
如有任何建议,我们将不胜感激。我是 android.
的新手
AlertDialog 有一个 getButton()
方法。您可以获得按钮并设置它的背景。有一些注意事项:您只能在显示对话框后调用该方法(我相信在对话框 class 上调用 onStart()
之后)。
也就是说,考虑到填充、边距、包含布局等,您可能不会得到想要的外观。AlertDialog
的要点是拥有一个与系统主题相匹配的标准外观对话框.它并不意味着可以自定义,UI-wise。
否则,您将需要创建一个完全自定义的对话框。为此,您有几个选项:DialogFragment、自定义对话框或对话框主题 activity。 Google 有关更多信息的主题。
我有一个警告对话框,目前看起来是这样的: Current Alert Dialog
但我希望 "YES" 和 "NO" 按钮有背景,或者看起来像按钮而不只是文本。我已经尝试为 AlertDialog 实现自定义主题,但我能得到的唯一更改是更改 "WARNING" 文本颜色。
自定义警报对话框的主题以便用实际按钮代替 "YES" 和 "NO" 的最有效和最简单的方法是什么?
这是我当前的 res/values/styles.xml 文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorAccent">@color/PCCA_blue</item>
</style>
<style name="customAlertDialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:textColor">@color/red</item>
</style>
这是我构建和显示 AlertDialog 的地方:
private void alert(final String action){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customAlertDialog));
String message = "";
switch(action) {
case "upload":
message = "Are you sure you want to upload the counted quantities?\n\nIt may take a few minutes to reflect your changes.";
break;
case "download":
message = "Downloading new worksheets may overwrite existing counted quantities, do you want to continue?\n\n" +
"NOTE: You may want to upload counts first.\n\n" +
"(Changed counted quantities will still be available for upload)";
break;
default:
message = action;
break;
}
alertDialogBuilder.setTitle("WARNING");
alertDialogBuilder.setMessage(message);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE) {
if (action.equals("upload")) {
MainActivity.upload(context);
} else if (action.equals("download")) {
MainActivity.download(context);
}
} else {
dialog.cancel();
}
}
};
alertDialogBuilder.setPositiveButton("YES", listener);
alertDialogBuilder.setNegativeButton("NO", listener);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
如有任何建议,我们将不胜感激。我是 android.
的新手AlertDialog 有一个 getButton()
方法。您可以获得按钮并设置它的背景。有一些注意事项:您只能在显示对话框后调用该方法(我相信在对话框 class 上调用 onStart()
之后)。
也就是说,考虑到填充、边距、包含布局等,您可能不会得到想要的外观。AlertDialog
的要点是拥有一个与系统主题相匹配的标准外观对话框.它并不意味着可以自定义,UI-wise。
否则,您将需要创建一个完全自定义的对话框。为此,您有几个选项:DialogFragment、自定义对话框或对话框主题 activity。 Google 有关更多信息的主题。