private void stripUnderlines(TextView textView) {
Spannable s = (Spannable) textView.getText();
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
// 替換為無下劃線 Span
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s); // 觸發重繪
}
// 自定義 Span
private static class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) { super(url); }
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false); // 關鍵:關閉下劃線
}
}
TextView email = findViewById(R.id.email);
email.setText("contact@example.com");
Linkify.addLinks(email, Linkify.EMAIL_ADDRESSES);
stripUnderlines(email); // 去除下劃線
// 同樣可設置顏色
email.setLinkTextColor(Color.WHITE);
String html = "contact@example.com";
textView.setText(Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY));
textView.setMovementMethod(LinkMovementMethod.getInstance());
HTML 方式可直接通過 CSS `text-decoration:none` 去除下劃線,但 Linkify 更適合純文本自動識別場景。