Android implements text links in textview There are many ways to implement text links in textview in Android. In summary, there are four types: 1. when the URL, email, and phone number appear in the text, you can set the Android: autolink attribute of textview to the corresponding value, if all types are available, Android: autolink = "all" is used ". Of course, you can alsoCodeTextview01.setautolinkmask (linkify. All ); 2. Write the text to be processed to a resource file, such as string. XML, and then reference it in the Java code (Directly Writing it in the Code is not feasible, and the text will be displayed and processed directly) 3. Use the fromhtml () method of the HTML class to format the text to be placed in the textview. 4. Use spannable or its class, such as spannablestring, to format some strings. Remember to add: Textview. setmovementmethod (linkmovementmethod. getinstance ()); Example: Setcontentview (R. layout. test ); String source = "<B> <font color = # ff0000> HTML view using textview" + "</Font> </B> <br> <a href = 'HTTP: // www.androidpeople.com '> androidpeople.com </a>" + "<Br> <a href = 'HTTP: // www.android.com '> android.com </a> "; Textview = (textview) findviewbyid (R. Id. textview01 ); Textview. settext (html. fromhtml (source )); // Used to enable links in textview. Textview. setmovementmethod (linkmovementmethod. getinstance ());
Or: Textview textview01 = (textview) findviewbyid (R. Id. textview01 ); Textview01.setautolinkmask (linkify. All ); String autolinktext = "http://student.csdn.net /? 232885 my csdn blog "; Textview01.settext (autolinktext ); Partial connections: Spannablestring Ss = new spannablestring ("Call: 4155551212 ."); SS. setspan (New stylespan (typeface. Bold), 0, 5, spanned. span_exclusive_exclusive ); SS. setspan (New urlspan ("Tel: 4155551212"), 6, 16, spanned. span_exclusive_exclusive ); Textview04.settext (SS ); Textview04.setmovementmethod (linkmovementmethod. getinstance ()); Only Part 1 is specified as a connection. |