android是否有可能在Java代码中使用带有基本标签的setText(text)函数为TextView提供一些文本,并使用 来标记下划线的单词?
答案 0 :(得分:34)
是的,您可以使用Html.fromhtml()方法:
textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
答案 1 :(得分:30)
将字符串定义为:
<resources>
<string name="your_string">This is an <u>underline</u> text demo for TextView.</string>
</resources>
答案 2 :(得分:8)
您可以使用SpannableString类中的UnderlineSpan:
SpannableString content = new SpannableString(<your text>);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
然后使用textView.setText(content);
答案 3 :(得分:3)
您可以在TextView上使用几乎所有HTML标记。查看示例here。
答案 4 :(得分:3)
tobeunderlined= <u>some text here which is to be underlined</u>
textView.setText(Html.fromHtml("some string"+tobeunderlined+"somestring"));
答案 5 :(得分:1)
TextView tv = findViewById(R.id.tv);
tv.setText("some text");
setUnderLineText(tv, "some");
还支持TextView子项,例如EditText,Button,Checkbox
public void setUnderLineText(TextView tv, String textToUnderLine) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToUnderLine, 0);
UnderlineSpan underlineSpan = new UnderlineSpan();
SpannableString wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToUnderLine, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}
如果需要
-可点击的下划线文字?
-是否在TextView的多个部分下划线?