I'm trying to make a alert dialog box that will ask the user a question and some of the text in the message will be lets say, red.
This is what I've tried:
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();
It's not changing the word "you" to the color red. Any ideas?
Here's one way to do it
In your 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();
In /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>
You can also set the TextColor using Html.fromHtml
title.setText(Html.fromHtml("<font color='red'>Test</font><font color='blue'>ing</font>"));
The title can also have a custom View for the title, you set it with setCustomTitle(View view)
没有自定义警报框,这是不可能做到的,如以下所示: http : //slayeroffice.com/code/custom_alert/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.