Android TextView, including strikethrough, hyperlinks, colors, and fonts, androidtextview

Source: Internet
Author: User

Android TextView, including strikethrough, hyperlinks, colors, and fonts, androidtextview
There are several ways to add special effects to TextView:

First, use the android: autolink attribute to automatically apply the effect, for example:
Java code

  1. <TextView xmlns: android = "http://schemas.android.com/apk/res/android"
  2. Android: id = "@ + id/text1"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: autoLink = "all"
  6. Android: text = "@ string/link_text_auto"
  7. />


Second, use the <a> label in the text, for example:
Java code
  1. <String name = "link_text_manual"> <B> text2: </B> This is some other
  2. Text, with a <a href = "http://www.google.com"> link </a> specified
  3. Via an & lt; a & gt; tag. Use a \ "tel: \" URL
  4. To <a href = "tel: 4155551212"> dial a phone number </a>
  5. </String>


The third and second types are actually the same, except that the text is changed to JAVA code, such:
Java code
  1. TextView t3 = (TextView) findViewById (R. id. text3 );
  2. T3.setText (
  3. Html. fromHtml (
  4. "<B> text3: </B> Text with a" +
  5. "<A href = \" http://www.google.com \ "> link </a>" +
  6. "Created in the Java source code using HTML ."));
  7. T3.setMovementMethod (LinkMovementMethod. getInstance ());


Fourth, the first three can be said to be automatic, and the fourth is pure "Manual. Create a SpanableString and create one or more spans on it to achieve a wide range of results. Example:
Java code
  1. SpannableString ss = new SpannableString ("text4: Click here to dial the phone .");
  2. Ss. setSpan (new StyleSpan (Typeface. BOLD), 0, 6, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
  3. Ss. setSpan (new URLSpan ("tel: 4155551212"), 13, 17, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
  4. TextView t4 = (TextView) findViewById (R. id. text4 );
  5. T4.setText (ss );
  6. T4.setMovementMethod (LinkMovementMethod. getInstance ());


For the complete code, see ApiDemo. The following are some notes:

. SetMovementMethod. This method is used to respond to user events. If you click a phone number, the dialing page is displayed. If you do not execute this method, it will not respond to the event, even if the text is already an underline blue.
. Spanned. SPAN_EXCLUSIVE_EXCLUSIVE: This is the flag that needs to be specified during setSpan. It means that I tried it for a long time and didn't try it. I had a sleep. This morning I suddenly thought about it. It is used to identify whether to apply new characters before and after the text within the Span range. Spanned. SPAN_EXCLUSIVE_EXCLUSIVE (not included before and after), Spanned. SPAN_INCLUSIVE_EXCLUSIVE (included first, not included later), Spanned. SPAN_EXCLUSIVE_INCLUSIVE (not included earlier, not included later), Spanned. SPAN_INCLUSIVE_INCLUSIVE (both before and after ). You can see the following:

 
Comparison
 



Below

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.