As a J2EE developer, I decided to write a blog today because I was inspired by the prevalence of plagiarism and reprinting on the Internet! It mainly records some experiences in the development process and small programming skills. Of course, I finally made up my mind to start blogging because of a problem that has been plagued by these days. Recently, I started Android development and want to edit text with hyperlinks in textview. It's actually quite simple. It can be implemented using spannablestring, But like other languages, the default hyperlink text in Android is blue and underlined, which is very ugly for us! So I went online to find a solution and asked Gu and Bai, who found that they provided almost the same article, no matter which website they were on .... Ah, let's explore it by yourself.
The following is a simple demo for your reference.
1. The first is the layout file, which is a simple textview.
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""
Android: textcolor = "#000000"
Android: gravity = "left"
/>
2. dynamically set content in Java code
Textview TV = (textview) findviewbyid (R. Id. textview );
Spannablestring text = new spannablestrig ("Baidu ");
Text. setspan (New urlspannounderline ("http://www.baidu.com"), spanned. span_exclusive_exclusive );
TV. settext (text );
TV. setmovementmethod (linkmovementmethod. getinstance ());
3. The custom urlspan inherits the urlspan and removes the underline.
Import Android. Graphics. color;
Import Android. Text. textpaint;
Import Android. Text. style. urlspan;
// Remove the underline from the custom urlspan
Public class urlspannounderline extends urlspan {
Public urlspannounderline (string URL ){
Super (URL );
}
@ Override
Public void updatedrawstate (textpaint DS ){
Super. updatedrawstate (DS );
DS. setunderlinetext (false );
DS. setcolor (color. Black );
}
}
Note: Do not set autolink in the layout file.