2011-12-06 48 views
6

我已經使用android alert alert builder來顯示一些用戶消息(String)作爲彈出框給用戶。如何使用警報對話框構建器在彈出框中顯示Html格式的文本?

現在,我的要求是消息框應該能夠支持此消息的HTML格式。包括,此用戶消息中可能會返回一個URL /鏈接。選擇此鏈接應將當前應用程序置於後臺並彈出手機瀏覽器,並將用戶轉到鏈接中的URL。關閉瀏覽器會將應用程序帶回前臺。

我該怎麼做?我可以使用相同的警報對話框構建器還是可用的其他選擇?

回答

10

如果給定的字符串包含以上所有的答案將不刪除HTML標籤一樣,等我試過刪除所有的標籤,這對我來說很好用

AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
builder.setTitle("Title"); 

LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.custom_dialog, null); 

TextView text = (TextView) layout.findViewById(R.id.text); 
text.setMovementMethod(LinkMovementMethod.getInstance()); 
text.setText(Html.fromHtml("<b>Hello World</b> This is a test of the URL <a href=http://www.example.com> Example</a><p><b>This text is bold</b></p><p><em>This text is emphasized</em></p><p><code>This is computer output</code></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>";)); 
builder.setView(layout); 
AlertDialog alert = builder.show(); 

和custom_dialog會是這樣的;

<TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#FFF" 
      /> 

上面的代碼將刪除所有的HTML標籤,並顯示實例爲點擊URL能中的所有其他指定的HTML格式的文本。

0

嘗試使用從android.text.Spannable

Spannable span = Spannable.Factory.getInstance().newSpannable("html text"); 
textView.setText(span); 
3

Spannable類創建自定義警告對話框,follow this link

1,創建您的對話框

一個佈局。在該佈局中創建文本視圖

b。使用setText(Html.fromHtml(Source text here)) 然後做

Linkify.addLinks(Textview, Linkify.ALL) 

2.inflate此佈局在您的警告對話框,如圖教程