Android Development Notes – high imitation Sina Weibo word processing (keyword highlighting, custom expression replacement and click event)

Let's take a look at the renderings. This is a Sina Weibo client I wrote in my spare time:

Today, let's talk about how to achieve the effect shown in the figure above. Here we need to use the spannablestring tool class. If you are not familiar with this class, you can first 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 - colorful textview: http://www.cnblogs.com/lichenwei/p/4612079.html

First, let's talk about the structure of sina Weibo messages. When obtaining Sina Weibo messages, we will find these things:

Topic: with ## as the beginning and end, such as # world reading day #, etc.

At: starts with @ and ends with a space, such as @ Sina Weibo, etc.

Web address: start with http: / /, for example http://www.baidu.com/ 。

Expression: with [] as the beginning and end, such as [smile], [HA HA], etc.

To find the above keywords in a 140 word text, regular expressions are undoubtedly used. Here I define four regular expressions:

Then match the text fields of microblog messages according to the regular matching method provided in my last two articles. Here are some tips to share with you:

When I first implemented this function, I used many while loops, first matching @, then matching topics, then matching expressions, and finally matching URLs. This is no problem at the implementation level, but we still need to pay attention to the running efficiency of the code when writing programs. Here are some tips:

1. We can first splice the regularities we want to match and separate them with "|", and then associate the regularities of each part with parentheses. Then we can set an index in the group method to locate the matching regular content. The index 0 represents all, 1 represents the first parenthesis, and so on.

2. For clickable span implementation, we need to do two processes

(1) in the source code of clickablespan, we can find two sentences (as shown in the figure below), which are underlined with self-determined colors and text

So here we need to deal with it. We inherit this class, then copy its methods, and define the colors and styles we want.

(2) since the keywords @ nickname, # topic #, http: / /, etc. can be clicked, we need to do some processing on textview and set its movementmethod. See the following code for details.

3. In matching expressions (mentioned below), one thing to note here is that when we get the bitmap object, we need to compress the object to make its size consistent with the font size of our textview.

Let's talk about the implementation of expression replacement. First, we need to download the expression we need and store it in our resource file. Because the microblog information returns [XX], XX here is Chinese, and the name of our resource file cannot be Chinese, so we can write a resource storage class and encapsulate it with a static map set, Take Chinese as a key to correspond to the resource ID of int type.

Where to get these expression resources, you can go to my previous article "batch download of network pictures based on Java": http://www.cnblogs.com/lichenwei/p/4610298.html

Well, this is the end of the article. If you have any questions or suggestions, you can leave me a message in the article comments.

Author: Li Chenwei source: http://www.cnblogs.com/lichenwei/ The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original link must be given in an obvious position on the article page. I'm reading the children's shoes on my blog. I think you have extraordinary bearing. There's a king's spirit in your conversation. You must do something in the future! Next to the word "recommendation", you can easily point it. It's accurate. I won't accept a penny; If you're not sure, you can come back to me!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>