2012-11-24 50 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格式化標題。 –

相關問題