Spannablestring to change the TextView in the HTTP protocol, the phone number, the color of the word you want to change, and add the onclick event

Source: Internet
Author: User

This is my own seal of the two classes, as long as the call can be used, quite simple:



Package Com.example.spanable_textview;


Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;


Import Com.example.spanable_textview. Mainactivity.spannablelistener;

This class is to get the Spannablestring object, and add the color, followed by the Click event


Import Android.graphics.Color;
Import android.text.Spannable;
Import android.text.SpannableString;
Import android.text.Spanned;
Import Android.text.style.ForegroundColorSpan;
Import Android.view.View;
Import Android.view.View.OnClickListener;


public class getspannablestring{
/**
*
* Content of @param s TextView
* @param str you want to change the string, in fact the main thing is to have his length
* @param where the Start_ started
* @param listener Listener
* @return
*/
This is what you want to change.
public static spannablestring Getclickablespan (charsequence s, String str,int Start_,onclicklistener listener) {


int start = Start_;
int end = Str.length ();


spannablestring spanableinfo = new Spannablestring (s);


Spanableinfo.setspan (new clickable (listener),
Start, End+start, spanned.span_exclusive_inclusive);
Spanableinfo.setspan (Color.Blue, start, End+start,
spanned.span_exclusive_inclusive);

return spanableinfo;
}
This is the network.
public static spannablestring Getclickablehttpspan (charsequence Charsequence,onclicklistener listener) {


spannablestring spanableinfo = new spannablestring (charsequence);
Pattern pattern =Pattern.compile ("(http[s]{0,1}|ftp)://[a-za-z0-9\\.\\-]+\\." ( [A-za-z] {2,4}) (: \\d+)? (/[a-za-z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?) | (www. [A-za-z0-9\\.\\-]+\\. ([a-za-z]{2,4}) (: \\d+)? (/[a-za-z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*) ");
Matcher Matcher =pattern.matcher (charsequence);
if (Matcher.find ()) {

System.out.println ("start=" +matcher.start ());
System.out.println ("end==" +matcher.end ());
Spanableinfo.setspan (new clickable (listener), Matcher.start (), Matcher.end (), spannable.span_exclusive_exclusive);
Spanableinfo.setspan (New Foregroundcolorspan (Color.Blue), Matcher.start (), Matcher.end (), spannable.span_exclusive _exclusive);

}


return spanableinfo;
}
This is the phone number.
public static spannablestring Getclickablephonespan (charsequence charsequence, Onclicklistener listener) {
Spannablestring spannablestring =new spannablestring (charsequence);

Pattern pattern = pattern.compile ("(\ \ (86\\))?" ( 13[0-9]|15[0-35-9]|18[0125-9]) \\d{8} ");
Matcher Matcher =pattern.matcher (charsequence);
if (Matcher. Find ()) {
Spannablestring.setspan (new clickable (listener), Matcher.start (), Matcher.end (), spannable.span_exclusive_ EXCLUSIVE);
Spannablestring. Setspan (New Foregroundcolorspan (Color.Blue), Matcher.start (), Matcher.end (), Spannable.SPAN_ exclusive_exclusive);

}
return spannablestring;
}
}





There is also a Clickablespan implementation class, mainly to achieve the click of Fresh Fruit

Package Com.example.spanable_textview;


Import Android.text.TextPaint;
Import Android.text.style.ClickableSpan;
Import Android.view.View;
Import Android.view.View.OnClickListener;


Class clickable extends Clickablespan implements Onclicklistener {
Private final View.onclicklistener Mlistener;


Public clickable (View.onclicklistener l) {
Mlistener = l;


}


@Override
public void OnClick (View v) {
Mlistener.onclick (v);


}


@Override
public void Updatedrawstate (Textpaint ds) {
Ds.setcolor (Ds.linkcolor);
Ds.setunderlinetext (FALSE); Remove the Underline
}


}

Here's my demo. If you are interested, you can see http://download.csdn.net/download/u012808234/8356749.

Spannablestring to change the TextView in the HTTP protocol, the phone number, the color of the word you want to change, and add the onclick event

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.