Sina Weibo third-party clients, the text content of special characters (such as Web page short link, # #话题, @ person name , etc.) to highlight color, need to use the following several regular expressions.
1 /**Short link Regular expression for Weibo*/2 Static FinalString regex_http = "http (s)?:/ /([a-za-z|\\d]+\\.) +[a-za-z|\\d]+ (/[a-za-z|\\d|\\-|\\+|_./?%=]*)? ";3 Static FinalString regex_at = "@[\\u4e00-\\u9fa5\\w\\-]+";4 Static FinalString regex_sharp= "# ([^\\#|.] +)#";5 Static FinalString regex_emoji= "\\[[a-za-z0-9\\u4e00-\\u9fa5]+\\]";
Next is the function highlighting the special field, in a previous essay "Android-textview keyword highlighting and hyperlinks" in the example, this example will be modified to make it suitable for the use of this project.
1 /**2 * Highlight some text3 * @paramContent Text Contents4 * @return5 */6 Public Staticspannablestring settexthighlight (String content) {7spannablestring result =Newspannablestring (content);8 9 if(Content.contains ("@")) {TenPattern p =Pattern.compile (regex_at); OneMatcher m =P.matcher (result); A while(M.find ()) { - intStart =M.start (); - intEnd =m.end (); the Result.setspan ( -(NewForegroundcolorspan (Color.parsecolor ("#33b5e5"))), - start, end, spannable.span_exclusive_inclusive); - } + } - + if(Content.contains ("#") ){ APattern p =Pattern.compile (regex_sharp); atMatcher m =P.matcher (result); - while(M.find ()) { - intStart =M.start (); - intEnd =m.end (); - Result.setspan ( -(NewForegroundcolorspan (Color.parsecolor ("#ff7d00"))), in start, end, spannable.span_exclusive_inclusive); - } to } + - if(Content.contains ("HTTP//") ){ thePattern p =Pattern.compile (regex_http); *Matcher m =P.matcher (result); $ while(M.find ()) {Panax Notoginseng intStart =M.start (); - intEnd =m.end (); the Result.setspan ( +(NewForegroundcolorspan (Color.parsecolor ("#33b5e5"))), A start, end, spannable.span_exclusive_inclusive); the } + } - $ returnresult; $}
The following is the application of the above method
1 spannablestring text = utils.settexthighlight (Weibo.gettext ()); 2 Holder.content.setText (text);
As follows
Nexus5 screenshot Small Tips:
Simultaneously press "Volume Down" and "power key" can quickly screenshot, together press these two keys 2 seconds to fix.