[]textview in Android Episode Response click event (spannablestring)

Source: Internet
Author: User

    • Project requirements

Click on a small piece of text in the TextView to play a dialog box


    • Failed solution

At first, it was using two textview horizontal layout, imagine, when the first TextView quickly occupied a row, still not changed line, the second TextView is likely to have a newline layout problem


    • With the problem of spannablestring

Small paragraph text has underline

Click on the text in TextView, the system will be treated as a URL, to the text of the clicked part plus a blue background


    • Solution Solutions

public class    Touchablespan extends Clickablespan {private Boolean mispressed;    private int mpressedbackgroundcolor;    private int mnormaltextcolor;    private int mpressedtextcolor; Public Touchablespan (int normaltextcolor, int pressedtextcolor, int pressedbackgroundcolor) {Mnormaltextcolor = no        Rmaltextcolor;        Mpressedtextcolor = Pressedtextcolor;    Mpressedbackgroundcolor = Pressedbackgroundcolor;    The public void setpressed (Boolean isSelected) {mispressed = isSelected;        } @Override public void Updatedrawstate (Textpaint ds) {super.updatedrawstate (DS);        Ds.setcolor (mispressed mpressedtextcolor:mnormaltextcolor); Ds.bgcolor = mispressed?        mpressedbackgroundcolor:0x00eeeeee;    Ds.setunderlinetext (FALSE); } @Overridepublic void OnClick (View widget) {//todo}} 
Class Linktouchmovementmethod extends Linkmovementmethod {private Touchablespan mpressedspan; @Override public boolean ontouchevent (TextView TextView, spannable spannable, motionevent event) {if (event.get            Action () = = Motionevent.action_down) {Mpressedspan = Getpressedspan (TextView, spannable, event);                if (Mpressedspan! = null) {mpressedspan.setpressed (true); Selection.setselection (spannable, Spannable.getspanstart (Mpressedspan), Spannable.getspanend (mPress            Edspan)); }} else if (event.getaction () = = Motionevent.action_move) {Touchablespan Touchedspan = Getpressedspan (            TextView, spannable, event);                if (Mpressedspan! = NULL && Touchedspan! = Mpressedspan) {mpressedspan.setpressed (false);                Mpressedspan = null;            Selection.removeselection (spannable); }}} else {if (Mpressedspan! =NULL) {mpressedspan.setpressed (false);            Super.ontouchevent (TextView, spannable, event);            } Mpressedspan = null;        Selection.removeselection (spannable);    } return true; } Touchablespan Getpressedspan (TextView TextView, spannable spannable, motionevent event) {int x = (int) event.        GetX ();        int y = (int) event.gety ();        X-= Textview.gettotalpaddingleft ();        Y-= Textview.gettotalpaddingtop ();        x + = Textview.getscrollx ();        Y + = textview.getscrolly ();        Layout layout = Textview.getlayout ();        int line = layout.getlineforvertical (y);        int off = Layout.getoffsetforhorizontal (line, x);        touchablespan[] link = spannable.getspans (off, off, Touchablespan.class);        Touchablespan touchedspan = null;        if (Link.length > 0) {touchedspan = link[0];    } return Touchedspan; }}

Finally, don't forget to add

Mstartpagetermscondition.setmovementmethod (New Linktouchmovementmethod ());



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.