Set some text backgrounds to highlight: Java code
- Public ClassHighlightactivityExtendsActivity {
- String STRs = "My heart is too messy. Just give me some blank space. ";
- Textview;
- IntStart = 3;
- IntEnd = 5;
- /** Called when the activity is first created .*/
- @ Override
- Public VoidOncreate (bundle savedinstancestate ){
- Super. Oncreate (savedinstancestate );
- Setcontentview (R. layout. high_light );
- Textview = (textview) findviewbyid (R. Id. textview );
- Spannablestringbuilder style =NewSpannablestringbuilder (STRs );
- Style. setspan (NewBackgroundcolorspan (color. Red), start, end, spannable. span_exclusive_exclusive );
- Textview. settext (style );
- }
- }
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
- PackageCom. justel. contact;
- ImportAndroid. App. activity;
- ImportAndroid. Graphics. color;
- ImportAndroid. OS. Bundle;
- ImportAndroid. Text. spannable;
- ImportAndroid. Text. spannablestringbuilder;
- ImportAndroid. Text. style. backgroundcolorspan;
- ImportAndroid. Text. style. foregroundcolorspan;
- ImportAndroid. widget. textview;
- Public ClassHighlightactivityExtendsActivity {
- String STRs = "My heart is too messy. Just give me some blank space. ";
- Textview;
- IntStart = 3;
- IntEnd = 5;
- /** Called when the activity is first created .*/
- @ Override
- Public VoidOncreate (bundle savedinstancestate ){
- Super. Oncreate (savedinstancestate );
- Setcontentview (R. layout. high_light );
- Textview = (textview) findviewbyid (R. Id. textview );
- Spannablestringbuilder style =NewSpannablestringbuilder (STRs );
- Style. setspan (NewBackgroundcolorspan (color. Red), start, end, spannable. span_exclusive_exclusive );
- Style. setspan (NewForegroundcolorspan (color. Red), 7,9, spannable. span_exclusive_inclusive );
- Textview. settext (style );
- }
- }
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.