48. Tips for clicking the Android tag TextView

Source: Internet
Author: User

48. Tips for clicking the Android tag TextView
48. Tips for clicking the Android tag TextView

ClickableSpan source code custom ClickableSpan TagTextView TagTextViewActivity github


In some page circles, every dynamic text is usuallyRich Text.

There is a kind of doping.Tag. For exampleTags Rich Text on Sina Weibo.

In addition, the most important thing is:Click these labels. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "tag_textview_2" src = "http://www.bkjia.com/uploads/allimg/151225/0411102Q7-1.png" title = "\"/>

This involvesClickableSpan.

ClickableSpan source code
/** * If an object of this type is attached to the text of a TextView * with a movement method of LinkMovementMethod, the affected spans of * text can be selected.  If clicked, the {@link #onClick} method will * be called. */public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance {    /**     * Performs the click action associated with this span.     */    public abstract void onClick(View widget);    /**     * Makes the text underlined and in the link color.     */    @Override    public void updateDrawState(TextPaint ds) {        ds.setColor(ds.linkColor);        ds.setUnderlineText(true);    }}

ClickableSpan source code is very short, mainlyExtended the updateDrawState function of CharacterStyle and provided an onClick Method.

We can see that the color of the ClickableSpan font followsLinkColorColor,It is underlined by default..

But the tag of Sina Weibo isThe color can be customized without being underlined..

The preceding results show that:You need to customize a ClickableSpan.

Custom ClickableSpan
Public class ClickableSpanNoUnderline extends ClickableSpan {private static final String TAG = "ClickableSpan"; private static final int NO_COLOR =-206; private int color; private OnClickListener onClickListener; public ClickableSpanNoUnderline (int color, onClickListener onClickListener) {super (); this. color = color; this. onClickListener = onClickListener;} public ClickableSpanNoUnderline (OnCli CkListener onClickListener) {this (NO_COLOR, onClickListener);}/*** Makes the text underlined and in the link color. ** @ param ds */@ Override public void updateDrawState (@ NonNull TextPaint ds) {super. updateDrawState (ds); // set the text color if (this. color = NO_COLOR) {ds. setColor (ds. linkColor);} else {ds. setColor (this. color);} ds. clearShadowLayer (); // remove the underline ds. setUnderlineText (false); ds. bgColor = Color. TRANSPARENT;}/*** Performs the click action associated with this span. ** @ param widget */@ Override public void onClick (View widget) {if (this. onClickListener! = Null) {this. onClickListener. onClick (widget, this);} else {Log. w (TAG, "listener was null") ;}/ *** callback interface, which calls back its onClick event * to indicate whether the external user is clicked */public interface OnClickListener
{/*** ClickableSpan is clicked ** @ param widget * @ param span */void onClick (View widget, T span );}}

updateDrawState()The method isSet color and underline.

OnClickListener: Defines a generic callback interface,Expand your ClickableSpanNoUnderline: If your tag needs to save some Id or Content, you can inherit ClickableSpanNoUnderline to implement your custom ClickableSpanNoUnderline, clickableSpanNoUnderline is implemented during the View layer callback. onClickListener <your custom ClickableSpanNoUnderline>.(If fuzzy, you can see the following TagTextView and TagTextViewActivity)


Write a custom TextView and use ClickableSpanNoUnderline.

Public class TagTextView extends TextView {private ClickableSpanNoUnderline. onClickListener onTagClickListener; public TagTextView (Context context) {super (context);} public TagTextView (Context context, AttributeSet attrs) {super (context, attrs);} public TagTextView (Context context, attributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} @ TargetApi (Build. VERSION_CODES.LOLLIPOP) public TagTextView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super (context, attrs, defStyleAttr, defStyleRes );} /*** Add the label ClickableSpan ** @ param tags * @ param content * @ return SpannableStringBuilder */public SpannableStringBuilder addTagClickableSpan (ArrayList
Tags, String content, ClickableSpanNoUnderline. OnClickListener onTagClickListener) {this. onTagClickListener = onTagClickListener; StringBuilder sbTag = new StringBuilder (); Map
Content2TagDict = new HashMap <> ();/*** add # */if (tags! = Null & tags. size ()> 0) {for (Tag tag: tags) {sbTag. append ("#"); sbTag. append (tag. getContent (); sbTag. append ("#"); sbTag. append (""); content2TagDict. put (tag. getContent (), tag) ;}} int tagLength = sbTag. toString (). length (); sbTag. append (content);/*** add color */SpannableStringBuilder sb = new SpannableStringBuilder (sbTag. toString (); if (tagLength> 0) {String s = sb. toString (); String [] mo Del = s. split ("#"); for (int I = 0; I <model. length-1; I ++) {/*** filter "" and "*/if ("". equals (model [I]) | "". equals (model [I]) continue; int index = s. indexOf (model [I]); int mLength = model [I]. length (); TagClickableSpan span = new TagClickableSpan (0xffFF4081, this. onTagClickListener); span. setContent (model [I]); Tag tag = content2TagDict. get (model [I]); if (tag! = Null & tag. getId ()! = Null) {span. setId (tag. getId ();}/*** set TagClickableSpan */sb. setSpan (span, index-1, index + mLength + 1, Spannable. SPAN_INCLUSIVE_INCLUSIVE) ;}} return sb ;}/ *** Tag ClickableSpan */public class TagClickableSpan extends ClickableSpanNoUnderline {private Long id; private String content; public TagClickableSpan (int color, OnClickListener onClickListener) {super (color, onClickListener);} public TagClickableSpan (OnClickListener onClickListener) {super (onClickListener);} public Long getId () {return id;} public void setId (Long id) {this. id = id;} public String getContent () {return content;} public void setContent (String content) {this. content = content ;}}}

According to the TagTextView I wrote above, ClickableSpanNoUnderline. OnClickListener is implemented here.

Public class TagTextViewActivity extends AppCompatActivity implements ClickableSpanNoUnderline. OnClickListener
{Private TagTextView tagTV; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. setContentView (R. layout. activity_tag_textview); this. tagTV = (TagTextView) this. findViewById (R. id. tag_text_view_ TV); this. initData ();} private void initData () {ArrayList
Tags = new ArrayList <> (); Tag tag1 = new Tag (); tag1.setId (2601L); tag1.setContent (""); Tag tag2 = new Tag (); tag2.setId (2602L); tag2.setContent ("can always"); Tag tag3 = new Tag (); tag3.setId (2603L); tag3.setContent ("Save You From Anything"); tags. add (tag1); tags. add (tag2); tags. add (tag3); String sign = "there is no bondage in this world ...... save You From Anything ...... "; this. tagTV. setText (this. tagTV. addTagClickableSpan (tags, sign, this); // when you click a link, you must set the MovementMethod object this. tagTV. setMovementMethod (LinkMovementMethod. getInstance (); // set the color after the click. Here the ClickableSpan click background is involved. this. tagTV. setHighlightColor (0xff8FABCC);}/*** ClickableSpan is clicked ** @ param widget * @ param span */@ Override public void onClick (View widget, TagTextView. tagClickableSpan span) {ToastUtil. show (this, span. getId () + ":" + span. getContent (), Toast. LENGTH_SHORT );}}


If the code is incomplete, you can go to the githubNo. 31. Of course, we also need Star, T.

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.