更改警报对话框的文本颜色
Change text color of alert dialog
我的应用程序中有一个用于下载音频说明的弹出窗口。我想要做的是将“确定”的默认文本颜色更改为蓝色。我尝试了一些东西,但没有用。这是我的代码:
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();
}
但是更改没有生效,谁能告诉我我做错了什么?
您有两种方法可以做到这一点
- Override default dialog.
//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()
- Create your own
custom
dialog
// 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();
您必须在 AlertDialog 构造函数中提供自定义样式 ID:
AlertDialog.Builder(Context context, int themeResId)
样式文件应该是这样的:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">#0000FF</item>
</style>
尝试在 show
之后调用 setTextColor
。参考如下:
show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
将您的方法更改为
private void showDownloadPgmPopup() {
android.app.AlertDialog.Builder builder = new android.app
.AlertDialog.Builder(getActivity(),R.style.AlertDialog);
...
..
.
}
并在 res/values/styles.xml 下添加新的 AlertDialog 样式
<style name="AlertDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#595959</item>
<item name="android:colorAccent">#1b5e20</item>
</style>
下面是这些更改的屏幕截图
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
上面的代码对我不起作用,但下面的代码起作用了:
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.Yellow));
简单地说,您只需使用以下代码更改 "OK" 文本:
Html.fromHtml("<font color='#0000FF'>OK</font>")
此代码将根据您添加的颜色值完全改变您的文本颜色,而 #0000FF 是蓝色。
我可以通过使用 SpannableString 代替普通字符串来设法将文本颜色更改为红色。
例子-
var okString = new SpannableString("OK");
okString.SetSpan(new ForegroundColorSpan(Color.Red), 0, Strings.Report.Length, 0);
然后我将变量 okString 传递给 setItems()。
尝试将 spannableString 传递给 setPositiveButton()。
使用 Material 组件库只需使用 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();
以下是如何使用 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))
}
我的应用程序中有一个用于下载音频说明的弹出窗口。我想要做的是将“确定”的默认文本颜色更改为蓝色。我尝试了一些东西,但没有用。这是我的代码:
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();
}
但是更改没有生效,谁能告诉我我做错了什么?
您有两种方法可以做到这一点
- Override default dialog.
//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()
- Create your own
custom
dialog
// 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();
您必须在 AlertDialog 构造函数中提供自定义样式 ID:
AlertDialog.Builder(Context context, int themeResId)
样式文件应该是这样的:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">#0000FF</item>
</style>
尝试在 show
之后调用 setTextColor
。参考如下:
show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
将您的方法更改为
private void showDownloadPgmPopup() {
android.app.AlertDialog.Builder builder = new android.app
.AlertDialog.Builder(getActivity(),R.style.AlertDialog);
...
..
.
}
并在 res/values/styles.xml 下添加新的 AlertDialog 样式
<style name="AlertDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#595959</item>
<item name="android:colorAccent">#1b5e20</item>
</style>
下面是这些更改的屏幕截图
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
上面的代码对我不起作用,但下面的代码起作用了:
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.Yellow));
简单地说,您只需使用以下代码更改 "OK" 文本:
Html.fromHtml("<font color='#0000FF'>OK</font>")
此代码将根据您添加的颜色值完全改变您的文本颜色,而 #0000FF 是蓝色。
我可以通过使用 SpannableString 代替普通字符串来设法将文本颜色更改为红色。
例子-
var okString = new SpannableString("OK");
okString.SetSpan(new ForegroundColorSpan(Color.Red), 0, Strings.Report.Length, 0);
然后我将变量 okString 传递给 setItems()。
尝试将 spannableString 传递给 setPositiveButton()。
使用 Material 组件库只需使用 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();
以下是如何使用 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))
}