2013-07-05 23 views
1

很簡單的問題:如何從TextView的樣式文本

  1. 我有一些TextView的

    TextView textView = (TextView) findViewById(R.id.textView1); 
    
  2. 我設置樣式的文本(與<b>標籤或任何其他標籤)

    textView.setText(Html.fromHtml("Simple text. <b>Bold text</b>. Simple text.")); 
    

問題:當我打電話給textView.getText()我收到Simple text. Bold text. Simple text.。某種程度上,<b>標籤已經消失。

如何從TextView中檢索帶格式的文本?

+0

你只是硬編碼字符串?爲什麼不把原始字符串保存在某個地方,比如static-final? – Karakuri

+1

我的猜測是,你將不得不使用Html.toHtml()(http://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned))和TextView.getEditableText ()as Editable是從「Spanned」派生出來的,它是爲Html.toHtml輸入的。 http://developer.android.com/reference/android/text/Spanned.html –

+0

@Karakuri有很多字符串,他們會在運行時更改。 – kolobok

回答

6

@Wand設備的假設(見問題的意見)是正確的

下面的代碼工作:

TextView textView = (TextView) findViewById(R.id.textView1); 
textView.setText(Html.fromHtml("Simple text. <b>Bold text</b>. Simple text."), TextView.BufferType.EDITABLE); 
String almostSameText = Html.toHtml(tv.getEditableText()).toString(); 

結果是:<p>Simple text. <b>Bold text</b>. Simple text</p>

相關問題