How to add some text of TextView to the background color:
spannable str = new Spannablestring ("#fdsfdfsdfdsfd #"); Matcher Matcher = Getemailpattern (). Matcher ((charsequence) str); while (Matcher.find ()) {int start = Matcher.start (); int End = Matcher.end (); Str.setspan (new Foregroundcolorspan (0xff1a5798), start, end,spannable.span_exclusive_exclusive) ;} Textview.settext (str);
Suppose you find the email link on the TextView and underline it:
Write an attribute class to inherit Clickablespan
public class Messagebundlespan extends Clickablespan {public enum linktype {email}private String mtext;private mainactivi Ty Mactivity;public Messagebundlespan (mainactivity activity, String text) {if (activity = = null) {throw new Nullpointerexc Eption ("activity is NULL");} mactivity = Activity;mtext = text;} @Overridepublic void Updatedrawstate (Textpaint ds) {Ds.setcolor (Mactivity.getresources (). GetColor (R.color.message_ Bundle_link));d S.setunderlinetext (true);} @Overridepublic void OnClick (View widget) {new Alertdialog.builder (mactivity). Settitle ("Title"). Setmessage (MText). Create (). Show ();}}
<span style= "White-space:pre" ></span>textview TextView = (TextView) Findviewbyid (R.id.textview); String str = "$#%$%$%[email protected]&*&*[email protected]"; Textview.settext (str); Spannablestringbuilder StringBuilder = new Spannablestringbuilder (Textview.gettext ()); Applyregexpattern (TextView, StringBuilder, Getemailpattern (), MessageBundleSpan.LinkType.EMAIL); Textview.settext (StringBuilder, TextView.BufferType.SPANNABLE); Textview.setmovementmethod (Linkmovementmethod.getinstance ());
private static Pattern Getemailpattern () {if (Sharppattern = = null) Sharppattern = Pattern.compile ("[\\w[.-]][email Protected][\\w[.-]]+\\. [\\w]+]; return sharppattern;} private void Applyregexpattern (TextView TextView, Spannablestringbuilder stringBuilder, pattern pattern, Messagebundlespan.linktype type) { Matcher Matcher = Pattern.matcher (Textview.gettext (). toString (). toLowerCase ( )); while (Matcher.find ()) { String text = Textview.gettext (). toString (). substring (Matcher.start (), Matcher.end ()); Stringbuilder.setspan (New Messagebundlespan (this, text), Matcher.start (), Matcher.end (), spannable.span_inclusive_ INCLUSIVE); } }
:
Android assumes that an email link to textview or edittext is underlined and can be displayed on a click-to-email connection