Look first:
Write a hyperlink-supported object:
/** objects displayed as hyperlinks */public class LinkInfo implements comparable<linkinfo>{ private String content; Private String type; Private String ID; Private Boolean bisface = false; Private Boolean bselected = false; public static final String email = "email"; public static final String WEBURL = "WEBURL"; public static final String PhoneNumber = "PhoneNumber"; private int startIndex; private int endIndex;}
This parsing is done for the following string:
String s = "(#大笑)%$%$%3434343434343$%$%[email protected] $dfsfsfsdffds ^15959224872) [Email protected]&& Fefrewafrewfjwio ([email protected] () () [email protected] ";
private static Pattern Email_pattern = patterns.email_address;private static pattern Phone_pattern = Patterns.phone; private static Pattern Weburl_pattern = Patterns.web_url;public static arraylist<linkinfo> parsestr (String Strlink) {if (Textutils.isempty (Strlink)) {return null; }arraylist<linkinfo> resultlist = new arraylist<linkinfo> (); Arraylist<linkinfo> infolist = null; try{infolist = new arraylist<linkinfo> (); <span style= "White-space:pre" ></span><strong>matcher Matcher = Email_pattern.matcher (StrLink); Find the location of the email in the string </strong> int begin = 0; while (Matcher.find ()) {int start = Matcher.start (); int end = Matcher.end (); LinkInfo info = new LinkInfo (); Info.setstartindex (start); Info.setendindex (end); Info.setcontent (Matcher.group ()); Info.settype (Linkinfo.email); Infolist.add (info); } <strong>matcher Matcher1 = Phone_pattern.matcher(strlink);//Find the position of the number in the string </strong> while (Matcher1.find ()) {int start = Matcher1.start (); int end = Matcher1.end (); LinkInfo info = new LinkInfo (); Info.setstartindex (start); Info.setendindex (end); Info.setcontent (Matcher1.group ()); Info.settype (Linkinfo.phonenumber); Infolist.add (info); }//(#大笑) pattern pattern = pattern.compile ("(\ \ (#\\s{1,2}\\))"); Matcher matcher2 = Pattern.matcher (strlink); while (Matcher2.find ()) {int start = Matcher2.start (); int end = Matcher2.end (); System.out.println ("====start=" +start+ "end=" +end+ "Match group=" +matcher2.group ()); LinkInfo info = new LinkInfo (); Info.setstartindex (start); Info.setendindex (end); Info.setcontent (Matcher2.group ()); Info.setface (TRUE); Infolist.add (info); } collections.sort (Infolist); int last = 0; for (int i=0;i<infolist.size (); i++) {LinkInfo info = infolist.get (i); if (Begin! = Info.getstartindex ()) {LinkInfo Infobefore = new LinkInfo (); Infobefore.setcontent (Strlink.substring (Begin,info.getstartindex ())); Resultlist.add (Infobefore); } resultlist.add (info); Begin = Info.getendindex (); Last = Info.getendindex (); } if (last < Strlink.length ()) {LinkInfo info = new LinkInfo (); Info.setcontent (Strlink.substring (Last,strlink.length ())); Resultlist.add (info); }}catch (Exception ex) {ex.printstacktrace (); } return resultlist;}
Layout of activity:
<?XML version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:background=" @drawable/w Hite "> <include android:layout_width=" wrap_content "android:layout_height=" Wrap_content "an Droid:layout_alignparentbottom= "true" android:id= "@+id/header" layout= "@layout/news_header"/> < ; Relativelayout android:id= "@+id/title" android:layout_above= "@id/header" android:layout_width= "Fill_ Parent "android:layout_height=" match_parent "android:background=" @drawable/white "android:gravity=" ce Nter_horizontal "> <com.kaixin001.view.introview android:id=" @+id/news_item_text "Andro Id:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_alignparenttop= "tr UE "ANDROID:LAYOUT_MARGINLeft= "30DP" android:layout_marginright= "30DP" android:layout_margintop= "20DP" Android:enab Led= "true" android:textcolor= "@drawable/black" android:textsize= "16SP" android:textstyle= "Bold"/> </RelativeLayout></RelativeLayout>
Introview has a method to load a string:
<pre name= "code" class= "Java" >public class Introview extends TextView {private arraylist<linkinfo> titlelist;private int displaywidth = 0;private float displayheight = 0;private float Curlen = 0;private Bitmap starBmp;pri vate Bitmap selectedbmp;private Float PosX = 0;private float PosY = 0;private linkinfo curinfo;//current clicked link object private Onclic Klinklistener listener;private String mfacetype = msg_face_type;public static final String Msg_face_type = "Msgtype";p UBL IC static final String Status_face_type = "Statustype";p ublic Introview (context context) {super (context);} Public Introview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Introview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} public void Settitlelist (arraylist<linkinfo> titlelist) {this.titlelist = Titlelist;displayheight = 0; Requestlayout ();}} <span style= "font-family:arial, Helvetica, Sans-serif;" > ≪/span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >activity in this way to call:</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
Introview News_item_text = (introview) Findviewbyid (R.id.news_item_text);
not supported in string \ n
String s = "(#大笑)%$%$%3434343434343$%$%[email protected] $dfsfsfsdffds ^15959224872) [Email protected]&& Fefrewafrewfjwio ([email protected] () () [email protected] ";
News_item_text.settitlelist (Parsenewsinfoutil.parsestr (s));
News_item_text.setonclicklinklistener (this);
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
</pre><pre code_snippet_id= "447070" snippet_file_name= "blog_20140810_12_4943450" name= "code" class= "Java" ><span style= "font-family:arial, Helvetica, Sans-serif;" >introview's theme was onmeasure in Measurewidth and Measureheight, to get arraylist<linkinfo>. Titlelist location information for each linkinfo. and get the height and width of this introview,</span>
And then OnDraw to draw each of Titlelist's linkinfo through a loop.
</pre><pre code_snippet_id= "447070" snippet_file_name= "blog_20140810_15_8956611" name= "code" class= "Java" ><strong><span style= "White-space:pre" ></span> @Override <span style= "White-space:pre" > </span>protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {<span style= "White-space:pre" ></span>super.onmeasure (Widthmeasurespec, heightmeasurespec); <span style= "White-space:pre" ></ Span>try{<span style= "White-space:pre" ></span>int width = measurewidth (widthmeasurespec); <span Style= "White-space:pre" ></span>int height = measureheight (heightmeasurespec); <span style= "White-space: Pre "></span>setmeasureddimension (width, height); <span style=" White-space:pre "></span>}catch (Exception ex) {<span style= "White-space:pre" ></span>ex.printstacktrace (); <span style= "White-space:pre" ></ Span>}<span style= "White-space:pre" ></span>}</strong>
<span Style= "Font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" > <span style= "White-space:pre" ></span></span><pre name= "code" class= "Java" > private int measurewidth (int measurespec) {int result = 0; int specmode = Measurespec.getmode (Measurespec); int specsize = measurespec.getsize (Measurespec); int initialwidth = Getpaddingleft () + getpaddingright (); int width = initialwidth; int maxWidth = 0; Textpaint temppaint = null; if (Specmode = = measurespec.exactly) {result = Specsize; } else {if (temppaint = = null) {Temppaint = new textpaint (); Temppaint.setstyle (Style.fill); Temppaint.setantialias (TRUE); Temppaint.settextsize (GetTextSize ()); } if (titlelist! = null && titlelist.size () > 0) {maxWidth = specsize;int size = Titlelist.size (); forlable:for (int i = 0; i < size; i++) { LinkInfo info = titlelist.get (i); if (Info.isface ()) {Bitmap facebmp = null; if (Mfacetype = = msg_face_type) {facebmp = Messagefacemodel.getinstance (). Getfaceicon (Info.getcontent ()); } if (facebmp! = null) {int wsize = Facebmp.getwidth () + 4; <strong> if (width + wsize >= maxWidth) </strong> {//here means that the width of the calculation is greater than the width of the control, then return maxWidth to be able to width = maxWidth; Break forlable; } width + = wsize; } continue; } String Text = Info.getcontent (); Text = Text.replaceall ("\ n", ""); Because the control does not support \ n, change this to a space if (! Textutils.isempty (text)) {Float wsize = temppaint.measuretext (text); if (width + wsize >= maxWidth) {width = maxWidth; Break forlable; } width + = wsize; }}} result = width; } displaywidth = result; return result; }
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" > </span>
Android assumes rewrite OnDraw implements a similar textview capable of displaying emoticons and linked controls (i)