Click on a small piece of text in the TextView to play a dialog box
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
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 ());