2012-11-24 52 views
1

我正在试图提出一个警告对话框,要求用户提出一个问题,并且该消息中的一些文本将被称为红色。更改警报对话框中的文本颜色?

这是我已经试过:

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(); 

它不改变的话“你”的红色。有任何想法吗?

回答

0

这是不可能没有的自定义警告框,这是这里出的事:http://slayeroffice.com/code/custom_alert/

+0

这似乎是一个网页上的提示对话框中设置它。 – antew

+0

是的,我意识到我的错误后,评论,但“删除”似乎只是要求投票删除评论,我不知道这是否是正确的行动方针。 – jcolicchio

1

这里有一个办法做到这一点

在你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(); 

在/布局/ 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

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

标题也可以为标题的自定义视图设置的文字颜色,你setCustomTitle(View view)

+0

我喜欢使用HTML格式化标题。 –

相关问题