Android implements the text link in TextView in four ways and the code

  • 2020-05-09 19:17:23
  • OfStack

  Android there are many ways to implement text linking in TextView.

To sum up, there are about four:
1. When URL, E-mail, telephone number, etc. appear in the text, android:autoLink property of TextView can be set to the corresponding value. If all the types appear, android:autoLink="all". You can also do it in the java code, textView01.setAutoLinkMask (Linkify.ALL);
2. The text to be processed is written to a resource file, such as string.xml, and then referenced in the java code (it is not feasible to write directly in the code, which will display the text directly).
3. Format the text to be placed in TextView using the fromHtml () method of class Html
4. Use Spannable or a class that implements it, such as SpannableString, to format partial strings.

Finally remember to add:
 
textView.setMovementMethod(LinkMovementMethod.getInstance()); 

Example:
 
[code] 
setContentView(R.layout.test); 
String source = "<b><font color=#ff0000> Html View using TextView" 
+ "</font></b><br><br><a href='http://www.AndroidPeople.com'>AndroidPeople.com</a>" 
+ "<br><br><a href='http://www.Android.com'>Android.com</a>"; 
TextView 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 connection:
 
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 4155551212 is specified as a connection

Related articles: