Android突出显示自定义textview中的链接文本

时间:2016-08-26 11:40:13

标签: android textview

我有自定义TextView来处理链接点击。我想在用户按下时突出显示链接。这是我的自定义TextView的代码。

package com.example.app.ui.extensions;

import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.style.ClickableSpan;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.example.app.helpers.LinkClickHelper;

public class LinkifyTextView extends TextView {
    public LinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public LinkifyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinkifyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        this.setAutoLinkMask(Linkify.WEB_URLS);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        final Object text = getText();
        if (text instanceof Spannable) {
            final Spannable buffer = (Spannable) text;
            final int action = event.getAction();

            if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= getTotalPaddingLeft();
                y -= getTotalPaddingTop();

                x += getScrollX();
                y += getScrollY();

                final Layout layout = getLayout();
                final int line = layout.getLineForVertical(y);
                final int off = layout.getOffsetForHorizontal(line, x);

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

                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        int start = buffer.getSpanStart(link[0]);
                        int end = buffer.getSpanEnd(link[0]);
                        CharSequence linkText = ((Spannable) text).subSequence(start, end);

                        LinkClickHelper.openLink((Activity) getContext(), linkText.toString());
                    } else {
                        Selection.setSelection(buffer,
                                buffer.getSpanStart(link[0]),
                                buffer.getSpanEnd(link[0]));
                    }
                }
            }
        }
        return true;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        this.setMovementMethod(null);
    }

}

这是布局代码

<com.example.app.ui.extensions.LinkifyTextView
        android:id="@+id/description_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:selectableItemBackground"
        android:clickable="true"
        android:ellipsize="end"        
        android:scrollHorizontally="true"
        android:textColor="@color/text_color_secondary_73"
        android:textColorLink="@color/link_selector"
        />

3 个答案:

答案 0 :(得分:0)

您可以使用setTextColor()。但是,此自定义Textview不包含setTextColor()方法。所以你必须使用setTextColor()方法的任何其他自定义textview。

答案 1 :(得分:0)

在TextView中使用它。它会检测到链接。

                    android:textColorHighlight="@color/yellow"
                    android:textColor="@color/black"
                    android:autoLink="link"
                    android:textColorLink="@color/blue"

答案 2 :(得分:0)

经过更多搜索后,我在代码中找到了问题所在。 this.setMovementMethod(LinkMovementMethod.getInstance()); this.setLinkTextColor(ContextCompat.getColorStateList(App.getContext(), R.color.link_selector)); 而且我还确保不要继续点击链接到超级类,并使用onTouch方法自己处理链接。 简短链接突出显示由系统处理,链接打开由我处理。以下是我的工作代码。

public class LinkifyTextView extends TextView {
    public LinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public LinkifyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinkifyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        this.setAutoLinkMask(Linkify.WEB_URLS);
        this.setMovementMethod(LinkMovementMethod.getInstance());
        this.setLinkTextColor(ContextCompat.getColorStateList(App.getContext(), R.color.link_selector));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final Object text = getText();
        if (text instanceof Spannable) {
            final Spannable buffer = (Spannable) text;
            final int action = event.getAction();

            if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= getTotalPaddingLeft();
                y -= getTotalPaddingTop();

                x += getScrollX();
                y += getScrollY();

                final Layout layout = getLayout();
                final int line = layout.getLineForVertical(y);
                final int off = layout.getOffsetForHorizontal(line, x);

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

                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        int start = buffer.getSpanStart(link[0]);
                        int end = buffer.getSpanEnd(link[0]);
                        CharSequence linkText = ((Spannable) text).subSequence(start, end);

                        LinkClickHelper.openLink((Activity) getContext(), linkText.toString());
                        //prevent opening link by system
                        return false;
                    } else {
                        super.onTouchEvent(event);
                        Selection.setSelection(buffer,
                                buffer.getSpanStart(link[0]),
                                buffer.getSpanEnd(link[0]));
                    }
                } else {
                    super.onTouchEvent(event);
                }
            } else {
                super.onTouchEvent(event);
            }
        }
        return true;
    }

}