Android Development notes-high imitation Sina Weibo Text Processing (realize keyword highlighting, replace custom expressions and add Click Event implementation), Android high imitation

Source: Internet
Author: User

Android Development notes-high imitation Sina Weibo Text Processing (realize keyword highlighting, replace custom expressions and add Click Event implementation), Android high imitation

 

Let's take a look at this Sina Weibo client that I copied in my spare time:

  

 

Let's talk about how to implement the results. The SpannableString tool class is used here. If you are not familiar with this class, read the two articles I wrote earlier:

Android Development notes-personalized TextView (Sina Weibo): http://www.cnblogs.com/lichenwei/p/4411607.html

Android Development notes-rich TextView: http://www.cnblogs.com/lichenwei/p/4612079.html

 

Let's talk about the structure of the Sina Weibo message. When we get the Sina Weibo message, we will find these things:

Topic: Start With #, for example, # World Day.

At: starts with @ and ends with a space, for example, @ Sina Weibo.

URL: starts with http: //, for example, http://www.baidu.com /.

Emoticons: take [] as the beginning and end, for example, [Smile] or [Haha.

 

To find out the above keywords in a text with 140 words, it is undoubtedly a regular expression. Here I have defined four regular expressions:

// Define the regular expression private static final String AT = "@ [\ u4e00-\ u9fa5 \ w] + "; // @ private static final String TOPIC = "# [\ u4e00-\ u9fa5 \ w] + #"; // ## topic private static final String EMOJI = "\ [[\ u4e00-\ u9fa5 \ w] + \]"; // private static final String URL = "http: // [-a-zA-Z0-9 + &#// %? = ~ _ |! :,.;] * [-A-zA-Z0-9 + & @ #/% = ~ _ |] "; // Url

Then, match the text fields of Weibo messages based on the regular expression matching method provided in the last two articles. Here are some tips:

At first, I used many while loops when implementing this function, First Matching @, then matching the topic, then matching the emoticon and finally matching the Url. This is certainly no problem at the implementation level, but we still need to pay attention to the code running efficiency when writing programs. Here are some tips:

1. We can splice the regular expressions we want to match and split them with "|". Then we can associate the regular expressions in each part with parentheses, then you can set indexes in the group method to locate the matching regular content. index 0 indicates all, 1 indicates the first bracket, and so on.

2. For ClickableSpan implementation, you can click a style. here we need to do two operations.

(1) In the source code of ClickableSpan, We can find two such sentences (for example,) with custom colors and underscores (_).

 

So here we need to process it. We will inherit this class and then rewrite its method to define the color and style we want.

 

 

1/** 2 * inherits the updateDrawState method of ClickableSpan, customize the required style 3 * @ author Rabbit_Lee 4*5 */6 public static class MyClickableSpan extends ClickableSpan {7 8 @ Override 9 public void onClick (View widget) {10 11} 12 13 @ Override14 public void updateDrawState (TextPaint ds) {15 super. updateDrawState (ds); 16 ds. setColor (Color. BLUE); 17 ds. setUnderlineText (false); 18} 19 20}

 

(2) because the keywords such as @ nickname, # topic #, http: // can be clicked, we need to process TextView and set its MovementMethod, see the following code for details.

3. When matching an expression (as mentioned below), we need to note that when we obtain a Bitmap object, we need to compress the object, make the size of TextView consistent with that of TextView.

  

1/** 2 * Set Weibo content style 3 * @ param context 4 * @ param source 5 * @ param textView 6 * @ return 7 */8 public static SpannableString getWeiBoContent (final Context context, string source, TextView textView) {9 SpannableString spannableString = new SpannableString (source); 10 11 // Set regular 12 Pattern pattern = Pattern. compile (REGEX); 13 Matcher matcher = pattern. matcher (spannableString); 14 15 if (matcher. find ()){ 16 // to achieve the text click effect, special processing 17 textView is required here. setMovementMethod (LinkMovementMethod. getInstance (); 18 // reset the regular position 19 matcher. reset (); 20} 21 22 while (matcher. find () {23 // you can obtain the matching Regular Expression (0 indicates all, 1 indicates the first bracket) 24 final String at = matcher according to the index of the group's Parentheses. group (1); 25 final String topic = matcher. group (2); 26 String emoji = matcher. group (3); 27 final String url = matcher. group (4); 28 29 // process @ symbol 30 if (! = Null) {31 // obtain the matching position 32 int start = matcher. start (1); 33 int end = start +. length (); 34 MyClickableSpan clickableSpan = new MyClickableSpan () {35 36 @ Override37 public void onClick (View widget) {38 // The jump user must be implemented here, replace 39 Toast with one Toast. makeText (context, "click User:" + at, Toast. LENGTH_LONG ). show (); 40} 41}; 42 spannableString. setSpan (clickableSpan, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); 43} 44 45 // Process topic # symbol 46 if (topic! = Null) {47 int start = matcher. start (2); 48 int end = start + topic. length (); 49 MyClickableSpan clickableSpan = new MyClickableSpan () {50 51 @ Override52 public void onClick (View widget) {53 Toast. makeText (context, "click topic:" + topic, Toast. LENGTH_LONG ). show (); 54} 55}; 56 spannableString. setSpan (clickableSpan, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); 57} 58 59 if (emoji! = Null) {60 int start = matcher. start (3); 61 int end = start + emoji. length (); 62 int ResId = EmotionUtils. getImgByName (emoji); 63 Bitmap bitmap = BitmapFactory. decodeResource (context. getResources (), ResId); 64 if (bitmap! = Null) {65 // obtain the character size 66 int size = (int) textView. getTextSize (); 67 // compress Bitmap68 bitmap = Bitmap. createScaledBitmap (bitmap, size, size, true); 69 // set emoticon 70 ImageSpan imageSpan = new ImageSpan (context, bitmap); 71 spannableString. setSpan (imageSpan, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); 72} 73} 74 75 // process url address 76 if (url! = Null) {77 int start = matcher. start (4); 78 int end = start + url. length (); 79 MyClickableSpan clickableSpan = new MyClickableSpan () {80 81 @ Override82 public void onClick (View widget) {83 Toast. makeText (context, "click url:" + url, Toast. LENGTH_LONG ). show (); 84} 85}; 86 spannableString. setSpan (clickableSpan, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); 87} 88} 89 90 return spannableString; 91}

 

 

 

Let's talk about the implementation of emoticon replacement. First, we need to download the emoticon we need and store it in our resource file. Because Weibo Returns [XX], XX here is Chinese, and the name of our resource file cannot be Chinese. Therefore, we can write a resource storage class and encapsulate it with a static Map set, use Chinese as the Key to correspond to the Int type resource ID.

1 package com. lcw. weibo. utils; 2 3 import java. io. serializable; 4 import java. util. hashMap; 5 import java. util. map; 6 7 import com. lcw. weibo. r; 8 9 @ SuppressWarnings ("serial") 10 public class EmotionUtils implements Serializable {11 12 public static Map <String, Integer> emojiMap; 13 14 static {15 emojiMap = new HashMap <String, Integer> (); 16 emojiMap. put ("[Smile]", R. drawable. d_hehe); 17 emojiMap. put ("[haha] ", R. drawable. d_hehe); 18 emojiMap. put ("[Xi]", R. drawable. d_xixi); 19 emojiMap. put ("[Haha]", R. drawable. d_haha); 20 emojiMap. put ("[love you]", R. drawable. d_aini); 21 emojiMap. put ("[Snoop]", R. drawable. d_wabishi); 22 emojiMap. put ("[Surprised]", R. drawable. d_chijing); 23 emojiMap. put ("[dizzy]", R. drawable. d_yun); 24 emojiMap. put ("[tears]", R. drawable. d_lei); 25 emojiMap. put ("[Response]", R. drawable. d_chanzui); 26 emojiMap. put ("[Crazy]", R. d Rawable. d_zhuakuang); 27 emojiMap. put ("[hum]", R. drawable. d_heng); 28 emojiMap. put ("[cute]", R. drawable. d_keai); 29 emojiMap. put ("[angry]", R. drawable. d_nu); 30 emojiMap. put ("[sweat]", R. drawable. d_han); 31 emojiMap. put ("[Shy]", R. drawable. d_haixiu); 32 emojiMap. put ("[heart]", R. drawable. emoji_0x2764); 33 emojiMap. put ("[sleeping]", R. drawable. d_shuijiao); 34 emojiMap. put ("[money]", R. drawable. d_qian); 35 emojiMap. put ("[sneer]", R. drawa Ble. d_touxiao); 36 emojiMap. put ("[laugh cry]", R. drawable. d_xiaoku); 37 emojiMap. put ("[doge]", R. drawable. d_doge); 38 emojiMap. put ("[Meow]", R. drawable. d_miao); 39 emojiMap. put ("[cool]", R. drawable. d_ku); 40 emojiMap. put ("[fading]", R. drawable. d_shuai); 41 emojiMap. put ("[Shut Up]", R. drawable. d_bizui); 42 emojiMap. put ("[contempt]", R. drawable. d_bishi); 43 emojiMap. put ("[flowers]", R. drawable. d_huaxin); 44 emojiMap. put ("[applaud]", R. drawable. D_guzhang); 45 emojiMap. put ("[sad]", R. drawable. d_beishang); 46 emojiMap. put ("[thinking]", R. drawable. d_sikao); 47 emojiMap. put ("[ill]", R. drawable. d_shengbing); 48 emojiMap. put ("[Kiss]", R. drawable. d_qinqin); 49 emojiMap. put ("[angry]", R. drawable. d_numa); 50 emojiMap. put ("[too happy]", R. drawable. d_taikaixin); 51 emojiMap. put ("[ignore you]", R. drawable. d_landelini); 52 emojiMap. put ("[Right hum]", R. drawable. d_youhengheng); 53 emojiMap. Put ("[left hum]", R. drawable. d_zuohengheng); 54 emojiMap. put ("[sh]", R. drawable. d_xu); 55 emojiMap. put ("[grievance]", R. drawable. d_weiqu); 56 emojiMap. put ("[vomit]", R. drawable. d_tu); 57 emojiMap. put ("[Poor]", R. drawable. d_kelian); 58 emojiMap. put ("[cool]", R. drawable. d_dahaqi); 59 emojiMap. put ("[eye widening]", R. drawable. d_jiyan); 60 emojiMap. put ("[Disappointed]", R. drawable. d_shiwang); 61 emojiMap. put ("[Top]", R. drawable. d_ding); 62 emojiMap. p Ut ("[question]", R. drawable. d_yiwen); 63 emojiMap. put ("[Sleepy]", R. drawable. d_kun); 64 emojiMap. put ("[catch a cold]", R. drawable. d_ganmao); 65 emojiMap. put ("[Bye-bye]", R. drawable. d_baibai); 66 emojiMap. put ("[black line]", R. drawable. d_heixian); 67 emojiMap. put ("[Sinister]", R. drawable. d_yinxian); 68 emojiMap. put ("[face hitting]", R. drawable. d_dalian); 69 emojiMap. put ("[Dumb]", R. drawable. d_shayan); 70 emojiMap. put ("[pig head]", R. drawable. d_zhutou); 71 emojiMap. Put ("[pandatv]", R. drawable. d_xiongmao); 72 emojiMap. put ("[Rabbit]", R. drawable. d_tuzi); 73} 74 75 public static int getImgByName (String imgName) {76 Integer integer = emojiMap. get (imgName); 77 return integer = null? -1: integer; 78} 79}

About these emoticon resources where to get, you can look at my previous article "based on Java batch download network pictures": http://www.cnblogs.com/lichenwei/p/4610298.html

Now the article is over. If you have any questions or suggestions, you can leave a comment in the article.

 

 

Author: Li chenwei
Source: http://www.cnblogs.com/lichenwei/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide the original article link clearly on the article page.
I am reading this kid's shoes from my blog. I think you are so angry that you have a secret of the king in your conversation. You will do something in the future! With the word "recommendation" next to it, you can just click it. it's accurate. I don't accept anything. If you're not sure, you can come back to me!

 

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.