Android RichText makes TextView easy to support rich text (image Imagespan, click Effect, etc. like QQ micro-mail chat) _android

Source: Internet
Author: User

Androidrichtext help to achieve like QQ, micro-letter, a textview in both words and expressions have the effect of the picture, the use of plug-in framework, code simple, strong scalability.

The underlying framework package has only four Java files,

Richtextwrapper: The TextView package class, which supports rich text, is constructed from the new Richtextwrapper (TextView v).

Rtmovementmethod: inherits from the Android native Linkmovementmethod, rewrites the Ontouchevent method, optimizes Clickspan (clicks the response the text area) the click Response.

Resolver: An interface, we need to implement a rich text type only need to write a corresponding Resolver rewrite its resolve method, in the resolve method to implement the rich Text style.

richtexts: A collection of common methods, classes, and interfaces. Here's a quick overview of how to use this framework:

Directly according to TextView new A richtextwrapper, then all things can be done with it;
richtextwrapper richtextwrapper = new Richtextwrapper ( TextView) Findviewbyid (R.id.tv_main));
For example QQ hope in TextView also have expression and picture then can write the corresponding two resolver to realize the function of separate, directly in the initialization of call Addresolver incoming can.
Richtextwrapper.addresolver (imageresolver.class,emotionresolver.class);
At the same time, if we want to click on the image to respond to events, you can use Setonrichtextlistener to process, parameters to pass into the corresponding resolver, here is Imageresolver, If we need to customize the xxresolver processing of rich text effects to implement response events, then long afferent xxresolver.class;
Richtextwrapper.setonrichtextlistener ( Imageresolver.class, New Richtexts.richtextclicklistener () {
@Override public
void Onrichtextclick (TextView V, String content) {
//where you can do the Click event Processing
//do something
}
});
Finally, you can set the text directly in

Addresolver can pass in any resolver, let TextView support the rich text type we need, and if you need to cancel support for a rich text type, just either pass in the resolver, for example, if you only need to support the expression, then Richtextwrapper.addresolver (Emotionresolver.class);
Do not need to do too much code changes, more flexible.

Then again, you can see another key point is how Resolver;resolver is implemented? Use a demo here for a simple example:

public class Imageresolver implements Resolver {public static final String img_match_regular = "\\[img] (\\w+) \\[/img]";
public static pattern = Pattern.compile (img_match_regular); The SP in the parameter is the TextView Content,extra is convenient for us to use an additional data, the default is empty, if we want to pass the data from the outside to here can be achieved by Richtextwrapper.putextra () Override public void Resolve (final TextView TextView, final spannable sp, sparsearray<object> Extra, Final richtexts . Richtextclicklistener listener) {Matcher Matcher = pattern.matcher (sp); final Context context = Textview.getcontext (); WH Ile (Matcher.find ()) {String content = Matcher.group (1); final Richtexts.taggedinfo info = new Richtexts.taggedinfo (
Matcher.start (), Matcher.end (), content);
if (listener!=null) {Richtexts.richtextclickspan span = new Richtexts.richtextclickspan (listener,info.content);
Sp.setspan (span, Info.start, info.end, spannable.span_exclusive_exclusive);
} Richtexts.stickerspan Testspan = new Richtexts.stickerspan (context, r.drawable.icon,100,100); Richtexts.setimagespAn (SP, info, testspan);
Textview.postinvalidate ();  }
}

The above example, Richtextclickspan is everyone if you need to achieve the Click Effect must be used, and stickerspan is to achieve the image effect, to the last clickable image of the resolver to achieve, of course, above is a casual introduction of an icon, In reality, you need to make some adjustments to yourself based on the content and the effect you expect.

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.