2016-05-06 112 views
0

我想知道是否有办法获得HTML标记。Android - Html编辑文本

例如:

finalString = " <font color='#2caeac'>@test</font>"; 
editetext.setText(Html.fromHtml(finalString)); 

现在,如果我想修改我的EditText这样的:

new_string = edittext.getText().toString(); // getting my "@test"` 
new_string = new_string+"<font color='red'>newString</font>"; 
edittext.settext(Html.fromHtml(new_string)); 

然后我new_string不赶 “finalString” 的颜色。

如何获取finalString的颜色?

回答

0

我想到了一个有点棘手的解决方案,其中涉及替换HTML文本中的标签。 解释在代码的评论中。

String finalString = "<font color='#2caeac'>@test</font>"; 
textView.setText(Html.fromHtml(finalString)); 

// get the text with html 
String new_string = Html.toHtml(textView.getText()); 
// the result will be something like <p dir="something">text_with_html_here</p> 
// get the layout direction (above api 17 you can't use getDirection()) 
final int direction = Character.getDirectionality(Locale.getDefault().getDisplayName().charAt(0)); 
// check if it's RTL 
boolean isRTL = direction == Character.DIRECTIONALITY_RIGHT_TO_LEFT || direction == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC; 
String layoutDirection = isRTL ? "rtl" : "ltr"; 
// replace raw tags added with toHtml() 
new_string = new_string.replace("<p dir=\"" + layoutDirection + "\">", "").replace("</p>", ""); 
// use the String 
new_string = new_string + "<font color='red'>newString</font>"; 
textView.setText(Html.fromHtml(new_string));