Android在自定义文本视图中突出显示链接文本

Android在自定义文本视图中突出显示链接文本,android,textview,Android,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 and

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

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"
        />

您可以使用setTextColor()。但此自定义Textview不包含setTextColor()方法。因此,您必须将任何其他自定义textview与setTextColor()方法一起使用

您可以使用setTextColor()。但此自定义Textview不包含setTextColor()方法。因此,您必须将任何其他自定义textview与setTextColor()方法一起使用

在文本视图中使用此选项。它将检测链接

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

在TextView中使用此选项。它将检测链接

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

经过更多的搜索,我在代码中找到了问题的地方。
this.setMovementMethod(LinkMovementMethod.getInstance());
this.setLinkTextColor(ContextCompat.getColorStateList(App.getContext(),R.color.link_选择器));
我还确保不会继续链接到超类,并使用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;
    }

}

经过更多的搜索,我在代码中找到了问题的地方。
this.setMovementMethod(LinkMovementMethod.getInstance());
this.setLinkTextColor(ContextCompat.getColorStateList(App.getContext(),R.color.link_选择器));
我还确保不会继续链接到超类,并使用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;
    }

}