Android-set the color and background of some textview text (highlighted)

Source: Internet
Author: User
Set some text backgrounds to highlight: Java code
  1. Public ClassHighlightactivityExtendsActivity {
  2. String STRs = "My heart is too messy. Just give me some blank space. ";
  3. Textview;
  4. IntStart = 3;
  5. IntEnd = 5;
  6. /** Called when the activity is first created .*/
  7. @ Override
  8. Public VoidOncreate (bundle savedinstancestate ){
  9. Super. Oncreate (savedinstancestate );
  10. Setcontentview (R. layout. high_light );
  11. Textview = (textview) findviewbyid (R. Id. textview );
  12. Spannablestringbuilder style =NewSpannablestringbuilder (STRs );
  13. Style. setspan (NewBackgroundcolorspan (color. Red), start, end, spannable. span_exclusive_exclusive );
  14. Textview. settext (style );
  15. }
  16. }
Public class highlightactivity extends activity {string STRs = "My mind is too messy. Just give me some blank space. "; Textview; int start = 3; int end = 5;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. high_light); textview = (textview) findviewbyid (R. id. textview); spannablestringbuilder style = new spannablestringbuilder (STRs); style. setspan (New backgroundcolorspan (color. red), start, end, spannable. span_exclusive_exclusive); textview. settext (style );}}

Display Effect:

Set both text and background highlighting: Java code

  1. PackageCom. justel. contact;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. Graphics. color;
  4. ImportAndroid. OS. Bundle;
  5. ImportAndroid. Text. spannable;
  6. ImportAndroid. Text. spannablestringbuilder;
  7. ImportAndroid. Text. style. backgroundcolorspan;
  8. ImportAndroid. Text. style. foregroundcolorspan;
  9. ImportAndroid. widget. textview;
  10. Public ClassHighlightactivityExtendsActivity {
  11. String STRs = "My heart is too messy. Just give me some blank space. ";
  12. Textview;
  13. IntStart = 3;
  14. IntEnd = 5;
  15. /** Called when the activity is first created .*/
  16. @ Override
  17. Public VoidOncreate (bundle savedinstancestate ){
  18. Super. Oncreate (savedinstancestate );
  19. Setcontentview (R. layout. high_light );
  20. Textview = (textview) findviewbyid (R. Id. textview );
  21. Spannablestringbuilder style =NewSpannablestringbuilder (STRs );
  22. Style. setspan (NewBackgroundcolorspan (color. Red), start, end, spannable. span_exclusive_exclusive );
  23. Style. setspan (NewForegroundcolorspan (color. Red), 7,9, spannable. span_exclusive_inclusive );
  24. Textview. settext (style );
  25. }
  26. }
Package COM. justel. contact; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. text. spannable; import android. text. spannablestringbuilder; import android. text. style. backgroundcolorspan; import android. text. style. foregroundcolorspan; import android. widget. textview; public class highlightactivity extends activity {string STRs = "My mind is too messy. Just give me some blank space. "; Textview; int start = 3; int end = 5;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. high_light); textview = (textview) findviewbyid (R. id. textview); spannablestringbuilder style = new spannablestringbuilder (STRs); style. setspan (New backgroundcolorspan (color. red), start, end, spannable. span_exclusive_exclusive); style. setspan (New foregroundcolorspan (color. red), 7,9, spannable. span_exclusive_inclusive); textview. settext (style );}}

Display Effect:

Parameter description:
Spannable. span_exclusive_exclusive
API Description: spans of Type span_exclusive_exclusive do not expand to include text inserted at either their starting or ending point. they can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
That is, the style of the new text in the original text header or tail is not affected by the original text style, the original text is highlighted, and the new text is not highlighted.

Spannable. span_exclusive_inclusive
Non-0-length spans of Type span_inclusive_exclusive expand to include text inserted at their ending point but not at their starting point. When 0-length, they behave like points.
That is, the style of the new text is affected by the style of the original text. The original text is highlighted at the end, and the new appended text is also highlighted.

Related Article

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.