Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Latest code ,pls see <a href="https://github.com/boiledwater/spanned" rel="nofollow noreferrer">link form github</a> <br/></p> <blockquote> <pre><code>message.setSpan(span, start, end, flags); </code></pre> </blockquote> <p>You need <strong>remove origin span</strong> before set new span. Please see below<br/> <a href="https://stackoverflow.com/questions/9164190/the-onclick-of-clickablespan-is-not-working-for-urlspan">The onClick() of ClickableSpan is not working for URLSpan?</a></p> <p>EDIT<BR/> You can capture <strong>any span click event</strong> by <code>extends LinkMovementMethod</code></p> <pre><code> import android.os.Handler; import android.os.Message; import android.text.Layout; import android.text.Selection; import android.text.Spannable; import android.text.method.LinkMovementMethod; import android.text.method.MovementMethod; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.TextView; import java.lang.ref.WeakReference; public class LinkMovementMethodExt extends LinkMovementMethod { public static final int LinkMovementMethod_Down = 1001; public static final int LinkMovementMethod_Up = 2002; private static LinkMovementMethod sInstance; private Class mSpanClass = null; private WeakReference&lt;Handler&gt; mWeakReference = null; public static MovementMethod getInstance(Handler handler, Class spanClass) { if (sInstance == null) { sInstance = new LinkMovementMethodExt(); ((LinkMovementMethodExt) sInstance).mWeakReference = new WeakReference&lt;&gt;(handler); ((LinkMovementMethodExt) sInstance).mSpanClass = spanClass; } return sInstance; } @Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); /** * get you interest span */ Object[] spans = buffer.getSpans(off, off, mSpanClass); if (spans.length != 0) { if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0])); MessageSpan obj = new MessageSpan(); obj.setObj(spans); obj.setView(widget); Handler handler = mWeakReference.get(); if (handler != null) { Message message = handler.obtainMessage(); message.obj = obj; message.what = LinkMovementMethod_Down; message.sendToTarget(); return true; } return false; } else if (action == MotionEvent.ACTION_UP) { Handler handler = mWeakReference.get(); if (handler != null) { MessageSpan obj = new MessageSpan(); obj.setView(widget); Message message = handler.obtainMessage(); message.obj = obj; message.what = LinkMovementMethod_Up; message.sendToTarget(); return true; } return false; } } } return super.onTouchEvent(widget, buffer, event); } public boolean canSelectArbitrarily() { return true; } public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode, KeyEvent event) { return false; } </code></pre> <p><strong>textView.setMovementMethod(LinkMovementMethodExt.getInstance());</strong></p> <hr> <p><strong>Edit for "android developer"</strong><br/> It is better way to add <strong>Handler</strong> property for <strong>LinkMovementMethodExt</strong> class.<br/> You can capture all Spanned which are delivered as Message object.<br/><br> Snip code in onTouchEvent method:<br/></p> <pre><code>Message message = Handler.obtainMessage(); message.obj = buffer.getSpans(off, off, Spanned.class);//get all spanned message.what = 111;//which value ,it is up to you message.sendToTarget(); //send message to you target handler </code></pre> <p>You can handler expected spanned in you handler class. May be it is flexible way to handle .<br/> Hope to help you.</p> <hr> <p><img src="https://i.stack.imgur.com/QDnqv.png" alt="enter image description here"></p> <p><br/> Above textview text is <code>&lt;a href='/a'&gt;aaaa&lt;/a&gt;123456&lt;a href='/b'&gt;bbbb&lt;/b&gt;7890</code><br/> I understand you requirement : <br/>Click 'aaaa',you want get it<code>s href value '/a', click 'bbbb',get it</code>s href '/b'; Do not trigger default action which is opened in web browser. <br/> If my understand is right, you can do like this:<br/></p> <ul> <li>Set LinkMovementMethod for textview, etc:<code>textview.setMovementMethod(LinkMovementMethodExt.getInstance(handler, URLSpan.class));</code></li> <li>Get interest span, here is URLSpan.<br/> In you handler <strong>handleMessage</strong> method, you can do like this:<br/></li> </ul> <blockquote> <pre><code>private Handler handler = new Handler() { public void handleMessage(Message msg) { int what = msg.what; if (what == 100) { Object[] spans = (Object[])msg.obj; for (Object span : spans) { if (span instanceof URLSpan) { System.out.println(((URLSpan) span).getURL()); } } } }; }; </code></pre> </blockquote> <hr> <p><a href="https://github.com/boiledwater/spanned" rel="nofollow noreferrer" title="Download demo code">Download demo code</a> <br/></p> <ul> <li><strong>MainActivity</strong> has <strong>color</strong> property which you can assign which color value as you like.</li> </ul> <p>How to do?</p> <ul> <li><p>Step1, get current click span.</p></li> <li><p>Step2, set BackgroundColorSpan for current click span</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload