gpt4 book ai didi

android - 更改警报对话框中的文本颜色?

转载 作者:行者123 更新时间:2023-11-30 03:54:41 25 4
gpt4 key购买 nike

我正在尝试制作一个警告对话框,它会询问用户一个问题,并且消息中的一些文本可以说是红色的。

这是我试过的:

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
this);
String You = "<font color='red'>you</font>?";
dlgAlert.setMessage("How are " + Html.fromHtml(You));
dlgAlert.setTitle("Mood");
dlgAlert.setPositiveButton("Good",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {


dialog.dismiss();
}
});
dlgAlert.setNeutralButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {

dialog.dismiss();
}
});
dlgAlert.setNegativeButton("Bad",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {

dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();

它并没有将“你”这个词变成红色。有任何想法吗?

最佳答案

这里有一个方法可以做到这一点

在您的 Activity 中:

LayoutInflater factory = LayoutInflater.from(this);
View dialog = factory.inflate(R.layout.example_dialog, null);
TextView title = (TextView) dialog.findViewById(R.id.message);
SpannableString text = new SpannableString("Test 123");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 1, 2, 0);
text.setSpan(new ForegroundColorSpan(Color.DKGRAY), 2, 3, 0);
text.setSpan(new ForegroundColorSpan(Color.CYAN), 3, 4, 0);

title.setText(text, BufferType.SPANNABLE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialog)
.setTitle("Title")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create()
.show();

在/layouts/example_dialog.xml 中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#F70000" />

</LinearLayout>

您还可以使用 Html.fromHtml 设置 TextColor

title.setText(Html.fromHtml("<font color='red'>Test</font><font color='blue'>ing</font>"));

标题也可以有一个标题的自定义 View ,你用setCustomTitle(View view)设置它

关于android - 更改警报对话框中的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com