Android assumes that an email link to textview or edittext is underlined and can be displayed on a click-to-email connection

Source: Internet
Author: User
Tags gettext

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

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.