博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现TextView中link的点击效果
阅读量:6088 次
发布时间:2019-06-20

本文共 6214 字,大约阅读时间需要 20 分钟。

朋友们,你们在TextView处理link的时候是不是一直被苦逼的android默认的方式困扰?每次点击link的时候,点击效果是整个textview来响应。非常烂吧?原因就不多赘述了。

那么以下这个控件就适合你了。 gitbub的链接:

好用的话。帮忙点个赞。

 

package com.zhang.linkclick;import com.test.zhang.R;import android.content.Context;import android.content.res.ColorStateList;import android.content.res.TypedArray;import android.graphics.Color;import android.os.Handler;import android.text.Layout;import android.text.Selection;import android.text.Spannable;import android.text.style.ClickableSpan;import android.text.style.ForegroundColorSpan;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.ViewConfiguration;import android.widget.TextView;/** *  * @author zhangji * */public class LinkClickTextView extends TextView {    private static final String TAG = "LinkClickTextView";    private ClickableSpan mSelectedLink;    private boolean mHasPerformedLongPress;    private CheckForLongPress mPendingCheckForLongPress;    private ForegroundColorSpan mForegroundColorSpan;    private UnsetLinkPressedState mUnsetLinkPressedState;    public LinkClickTextView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public LinkClickTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        setLinksClickable(false);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinkClickTextView, defStyle, 0);        ColorStateList titleColor = a.getColorStateList(R.styleable.LinkClickTextView_textColorLinkClick);        if (titleColor != null) {            mForegroundColorSpan =new ForegroundColorSpan(titleColor.getColorForState(EMPTY_STATE_SET, Color.RED));        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        boolean handled = handledLinkTouch(event);        if (handled) {            return true;        } else {            return super.onTouchEvent(event);        }    }    @Override    public void cancelLongPress() {        removeLongPressCallback();        super.cancelLongPress();    }    @Override    protected void onDetachedFromWindow() {        removeLongPressCallback();        super.onDetachedFromWindow();    }    private boolean handledLinkTouch(MotionEvent event) {        CharSequence text = getText();        int pointCount = event.getPointerCount();        if (!(text instanceof Spannable) || pointCount > 1) {            return false;        }        int action = event.getAction();        Spannable buffer = (Spannable) text;        switch (action) {            case MotionEvent.ACTION_DOWN:                int x = (int) event.getX();                int y = (int) event.getY();                x -= this.getTotalPaddingLeft();                y -= this.getTotalPaddingTop();                x += this.getScrollX();                y += this.getScrollY();                Layout layout = this.getLayout();                int line = layout.getLineForVertical(y);                int off = layout.getOffsetForHorizontal(line, x);                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);                if (link.length != 0) {                    checkForLongClick(0);                    mSelectedLink = link[0];                    setLinkPressed(true);                    return true;                } else {                    mSelectedLink = null;                }                break;            case MotionEvent.ACTION_MOVE:                if (mSelectedLink != null) {                    return true;                }                break;            case MotionEvent.ACTION_UP:                if (mSelectedLink != null) {                    if (!mHasPerformedLongPress) {                        // This is a tap, so remove the longpress check                        removeLongPressCallback();                        mSelectedLink.onClick(this);                    }                    if (mUnsetLinkPressedState == null) {                        mUnsetLinkPressedState = new UnsetLinkPressedState();                    }                    postDelayed(mUnsetLinkPressedState,                            ViewConfiguration.getPressedStateDuration());                    mSelectedLink = null;                    return true;                }                break;            case MotionEvent.ACTION_CANCEL:                removeLongPressCallback();                break;            default:                break;        }        return false;    }    private void checkForLongClick(int delayOffset) {        if (isLongClickable()) {            mHasPerformedLongPress = false;            if (mPendingCheckForLongPress == null) {                mPendingCheckForLongPress = new CheckForLongPress();            }            mPendingCheckForLongPress.rememberWindowAttachCount();            postDelayed(mPendingCheckForLongPress,                    ViewConfiguration.getLongPressTimeout() - delayOffset);        }    }    private void removeLongPressCallback() {        if (mPendingCheckForLongPress != null) {            removeCallbacks(mPendingCheckForLongPress);        }    }    class CheckForLongPress implements Runnable {        private int mOriginalWindowAttachCount;        public void run() {            if (mOriginalWindowAttachCount == getWindowAttachCount()) {                if (performLongClick()) {                    mHasPerformedLongPress = true;                }            }        }        public void rememberWindowAttachCount() {            mOriginalWindowAttachCount = getWindowAttachCount();        }    }    private void setLinkPressed(boolean pressed) {        if (!(getText() instanceof Spannable) || mForegroundColorSpan == null) {            return;        }        Spannable buffer = (Spannable) getText();        if (buffer == null) {            return;        }        if (pressed) {            buffer.setSpan(mForegroundColorSpan, buffer.getSpanStart(mSelectedLink),                    buffer.getSpanEnd(mSelectedLink), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);        } else {            buffer.removeSpan(mForegroundColorSpan);        }    }    private final class UnsetLinkPressedState implements Runnable {        public void run() {            setLinkPressed(false);        }    }}

转载地址:http://wepwa.baihongyu.com/

你可能感兴趣的文章
centos7下配置tomcat
查看>>
in和exists的区别以及exists和distinct去重的区别?
查看>>
一次关于汽车的想象
查看>>
MYSQL的mysqldump+binlog备份
查看>>
运维工具
查看>>
laravel数据库查询是use方法的使用
查看>>
Saltstack-12:prod生产环境部署keepalived
查看>>
python数据结构与算法(5)
查看>>
Java 多线程编程
查看>>
软链接和硬链接的区别
查看>>
理解eosio.token合约
查看>>
详解MongoDB复制集
查看>>
关于tomcat的8005端口启动不起来的解决办法
查看>>
常量指针-指向常量的指针,指针常量-指针本身是常量,常量-不能更改值的常量,数组指针-是指针int (*p)[n] 指针数组-是数组int *p[n]...
查看>>
谈谈Java引用和Threadlocal的那些事
查看>>
spark的持久化和共享变量
查看>>
migration vmware vms to openstack kvm 修改vmware windows scsi to ide
查看>>
Centos7下更改docker镜像存放地址
查看>>
大数据处理的基本流程
查看>>
CDH5.16.1集群企业真正离线部署
查看>>