ClickableSpan和TouchableSpan:

来源:互联网 发布:生活大爆炸11季 知乎 编辑:程序博客网 时间:2024/04/29 15:54
 我是这样做的,
以下是我的ClickableSpan.java和TouchableSpan.java代码:
  1. import android.text.TextPaint;
  2. import android.text.style.CharacterStyle;
  3. import android.text.style.UpdateAppearance;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. /**
  7. * If an object of this type is attached to the text of a TextView
  8. * with a movement method of LinkTouchMovementMethod, the affected spans of
  9. * text can be selected.  If touched, the {@link #onTouch} method will
  10. * be called.
  11. */
  12. public abstract class TouchableSpan extends CharacterStyle implements UpdateAppearance     {
  13.     /**
  14.      * Performs the touch action associated with this span.
  15.      * @return  
  16.      */
  17.     public abstract boolean onTouch(View widget, MotionEvent m);
  18.     /**
  19.      * Could make the text underlined or change link color.
  20.      */
  21.     @Override
  22.     public abstract void updateDrawState(TextPaint ds);
  23. }
复制代码

  然后,我让LinkTouchMovementMethod类继承LinkMovementMethod类;onTouchEvent()方法中,onClick变成了onTouch:
  1. import android.widget.TextView;
  2. public class LinkTouchMovementMethod extends LinkMovementMethod
  3. {
  4.     @Override
  5.     public boolean onTouchEvent(TextView widget, Spannable buffer,
  6.                             MotionEvent event) {
  7.         int action = event.getAction();
  8.         if (action == MotionEvent.ACTION_UP ||
  9.             action == MotionEvent.ACTION_DOWN) {
  10.             int x = (int) event.getX();
  11.             int y = (int) event.getY();
  12.             x -= widget.getTotalPaddingLeft();
  13.             y -= widget.getTotalPaddingTop();
  14.             x += widget.getScrollX();
  15.             y += widget.getScrollY();
  16.             Layout layout = widget.getLayout();
  17.             int line = layout.getLineForVertical(y);
  18.             int off = layout.getOffsetForHorizontal(line, x);
  19.             TouchableSpan[] link = buffer.getSpans(off, off, TouchableSpan.class);
  20.             if (link.length != 0) {
  21.                 if (action == MotionEvent.ACTION_UP) {
  22.                     link[0].onTouch(widget,event); //////// CHANGED HERE
  23.                 } else if (action == MotionEvent.ACTION_DOWN) {
  24.                     link[0].onTouch(widget,event); //////// ADDED THIS
  25.                     Selection.setSelection(buffer,
  26.                                            buffer.getSpanStart(link[0]),
  27.                                            buffer.getSpanEnd(link[0]));
  28.                 }
  29.                 return true;
  30.             } else {
  31.                 Selection.removeSelection(buffer);
  32.             }
  33.         }
  34.         return super.onTouchEvent(widget, buffer, event);
  35.     }
  36. }
复制代码

然后,在你的MovementMethod中做适当的更改:
  1. TextView tv = (TextView) findViewById(R.id.tv);
  2. tv.setMovementMethod(new LinkTouchMovementMethod());
  3. Now to show the text:
  4. touchableSpan = new TouchableSpan() {
  5.     public boolean onTouch(View widget, MotionEvent m) {
  6.         ...
  7.     }
  8.     public void updateDrawState(TextPaint ds) {
  9.         ds.setUnderlineText(false);
  10.         ds.setAntiAlias(true);
  11.     }
  12. };
  13. String rv = "Text to span";
  14. text = new SpannableString(rv);
  15. text.setSpan(touchableSpan, begin, end, 0);
  16. tv.setText(text, BufferType.SPANNABLE);
复制代码

原创粉丝点击