我在我的应用程序中有一个用于下载音频指令的弹出窗口。我想要做的是我想要更改" OK"的默认文本颜色。到蓝色。我试了一下,但它不起作用。这是我的代码:
private void showDownloadPgmPopup() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity());
builder.setTitle("Download instructional audio?");
builder.setMessage(ParamConstants.AUDIODOWNLOADPERMISSION);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel);
goToMoveScreen();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AndroidDownloadFileByProgressBarActivity.StartAudioAssetDownload(getActivity());
}
}).create();
// change the text color of download instruction ok button
final android.app.AlertDialog dialog = builder.show();
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#ff5722"));
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
但改变并没有反映任何人都可以告诉我我做错了什么?
答案 0 :(得分:22)
您必须在AlertDialog构造函数中提供自定义样式ID:
AlertDialog.Builder(Context context, int themeResId)
,样式文件应该是这样的:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">#0000FF</item>
</style>
答案 1 :(得分:9)
答案 2 :(得分:5)
您有两种方法可以执行此操作
- 覆盖默认对话框。
醇>
//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Exit", errorMessage);
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
...
}
}).create();
//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#f34235"));
}
}
dialog.show()
- 创建您自己的
醇>custom
对话框
// create instance of dialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// get inflater and inflate layour for dialogue
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
// now set layout to dialog
dialogBuilder.setView(dialogView);
// create instance like this OR directly mentioned in layout
Button button= (Button) dialogView.findViewById(R.id.label_field);
button.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
// show dialog
alertDialog.show();
答案 3 :(得分:5)
尝试在setTextColor
之后调用show
。请参阅以下内容:
show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
答案 4 :(得分:0)
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
上面的代码对我不起作用,但是下面的代码对我有用:
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.Yellow));
答案 5 :(得分:0)
只需,您只需使用以下代码更改“确定”文本:
Html.fromHtml("<font color='#0000FF'>OK</font>")
当#0000FF为蓝色时,此代码将完全根据您添加的颜色值更改文本颜色。
答案 6 :(得分:0)
我可以通过使用 SpannableString 代替普通字符串来将文本颜色更改为红色。
示例-
var okString = new SpannableString("OK");
okString.SetSpan(new ForegroundColorSpan(Color.Red), 0, Strings.Report.Length, 0);
然后我将变量 okString 传递给setItems()。
尝试将spannableString传递给setPositiveButton()。
答案 7 :(得分:0)
使用Material Components库,只需使用buttonBarPositiveButtonStyle
属性定义自定义样式:
<!-- Alert Dialog -->
<style name="MaterialAlertDialog_OK_color" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle.textColor</item>
</style>
<style name="PositiveButtonStyle.textColor" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/......</item>
</style>
然后:
new MaterialAlertDialogBuilder(context,
R.style.MaterialAlertDialog_OK_color)
.setMessage("Message......")
.setPositiveButton("ok", null)
.setNegativeButton("Cancel", null)
.show();
答案 8 :(得分:0)
在这里,您可以使用MaterialAlertDialogBuilder
来显示和自定义AlertDialog。
如果您想了解更多详细信息,请务必在此处查看。 https://www.journaldev.com/309/android-alert-dialog-using-kotlin
val builder = MaterialAlertDialogBuilder(requireContext())
with(builder) {
setTitle(R.string.log_out)
setMessage(R.string.dialog_msg_logout)
setPositiveButton(R.string.confirm) { _, _ ->
logoutUser()
}
setNegativeButton(R.string.cancel, null)
}
val dialog = builder.create()
dialog.show()
val button = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
with(button) {
setTextColor(ContextCompat.getColor(requireContext(), R.color.colorAccent))
}