實現TextView中link的點擊效果,textviewlink

來源:互聯網
上載者:User

實現TextView中link的點擊效果,textviewlink

朋友們,你們在TextView處理link的時候是不是一直被苦逼的android預設的方式困擾?每次點擊link的時候,點擊效果是整個textview來響應。很爛吧?原因就不多贅述了。

那麼下面這個控制項就適合你了。 gitbub的連結:https://github.com/zhangjizxc/LinkClickTextView


好用的話,幫忙點個贊。 


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);        }    }}



怎給textview添加一個點擊事件響應?

TextView txt = (TextView) findViewById(R.id.yourTextBoxId)); txt.setOnClickListener(new OnClickListener() { public void onClick(View view) { // USer Clicked the textBox } });
 
android TextView點擊事件

不會啊,剛試了, 在setOnClickListener上面下面加textView.setAutoLinkMask(Linkify.ALL)都可點,是導的View.OnClickListener嗎?
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.