Android 实现clickspan显示及点击效果

511 查看

在很多社交应用中需要实现类似新浪微博的@某某某的文本显示及点击效果。
如下图

下面来说下具体的实现步骤。

设置颜色及处理点击事件

设置颜色及点击事件处理需要自定义clickspan,代码如下:

public class WeiBoContentClickableSpan extends ClickableSpan {

private Context mContext;

public WeiBoContentClickableSpan(Context context) {
    mContext = context;
}

@Override
public void onClick(View widget) {

}

@Override
public void updateDrawState(TextPaint ds) {
    super.updateDrawState(ds);
    ds.setColor(0xff507daf);
    ds.setUnderlineText(false);
}
}

设置点击效果

实现点击效果需要自定义LinkMovementMethod类,代码如下:

/**
 * Created by pkhope on 2016/6/16.
 */
public class MyLinkMovementMethod extends LinkMovementMethod {

    private static LinkMovementMethod sInstance;

    public static MovementMethod getInstance() {
        if (sInstance == null)
            sInstance = new MyLinkMovementMethod();

        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 ||
                action == MotionEvent.ACTION_MOVE) {
            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);

            ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);

                    buffer.setSpan(new BackgroundColorSpan(Color.TRANSPARENT),
                            buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    Selection.removeSelection(buffer);

                } else if (action == MotionEvent.ACTION_DOWN) {

                    buffer.setSpan(new BackgroundColorSpan(Color.GRAY),
                            buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    Selection.setSelection(buffer,
                            buffer.getSpanStart(link[0]),
                            buffer.getSpanEnd(link[0]));
                } else if (action == MotionEvent.ACTION_MOVE){

                    buffer.setSpan(new BackgroundColorSpan(Color.TRANSPARENT),
                            buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    Selection.removeSelection(buffer);
                }

                return true;
            } else {
                Selection.removeSelection(buffer);
            }
        }
//        return super.onTouchEvent(widget, buffer, event);
        return false;
    }
}

总结

设置点击效果的关键点是处理点击后背景色不消失的问题。
具体代码可以参考项目 https://github.com/pkhope/weibo