2017-10-07 96 views
1

我希望我的alertDialog這個樣子 enter image description here如何顯示與顏色alertDialog.setMessage()在Android的

我的API是26字符串變量,這是我到目前爲止的代碼

alertDialog.setTitle("Warning"); 
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ 9.00 </b></font>",Build.VERSION.SDK_INT)); 

正如你所看到的,我只是把9.00直接放在字符串中。 現在,我想讓9.00一個名爲estCost的字符串變量,可以根據我的代碼中的計算進行更改。

如何在alertDialog.setMessage()中顯示「顏色字符串變量」estCost?

我曾嘗試下面的代碼

String estCost = calculate(10) //calculate method will return a double in string. 
alertDialog.setTitle("Warning"); 
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var> estCost </var></b></font>",Build.VERSION.SDK_INT)); 

但它不工作。請幫幫我 !謝謝。

+1

'「+ estCost +」' –

+1

Dope !!!!!! 我節省了我的一天兄弟〜 我已經在互聯網上搜索它。 你剛殺死它真的很快〜謝謝 –

回答

0

你需要使用連接運算符來Concat的值「+」

alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var>"+ estCost+" </var></b></font>",Build.VERSION.SDK_INT)); 
0

使用spannable文本

Spannable myText = new SpannableString("are u sure.....");   
myText.setSpan(new ForegroundColorSpan(Color.RED),7,10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
alertDialog.setMessage(myText); 
0

您需要Concat的字體+字體標記之前和字體經過這麼用標籤就是這些。