TextView可以选择并包含链接吗?

时间:2013-04-05 14:19:41

标签: android textview

我遇到了TextView的问题。我可以使用setTextIsSelectable(true)选择它,但是当我通过setMovementMethod(LinkMovementMethod.getInstance())启用链接时,它就不再可以选择了。

请注意,我并不是说可以点击原始链接,而是通过使用TextView之类的内容加载带有HTML标记的setText(Html.fromHtml("<a href='http://stackoverflow.com'>Hello World!</a>"))来使实际单词可以点击。

8 个答案:

答案 0 :(得分:25)

oakes's在文本视图上双击导致异常

java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0...

我查看了 LinkMovementMethod 中的 onTouchEvent 实现,发现当textview不包含链接时会删除选择。在这种情况下,当用户尝试更改时,选择从空值开始,应用程序崩溃。

...
if (link.length != 0) {
    if (action == MotionEvent.ACTION_UP) {
        link[0].onClick(widget);
    } else if (action == MotionEvent.ACTION_DOWN) {
        Selection.setSelection(buffer,
        buffer.getSpanStart(link[0]),
        buffer.getSpanEnd(link[0]));
    }
  return true;
} else {
  Selection.removeSelection(buffer);
}
...

所以我重写 onTouchEvent 方法,它运行正常。

public class CustomMovementMethod extends LinkMovementMethod {
    @Override
    public boolean canSelectArbitrarily () {
        return true;
    }

    @Override
    public void initialize(TextView widget, Spannable text) {
        Selection.setSelection(text, text.length());
    }

    @Override
    public void onTakeFocus(TextView view, Spannable text, int dir) {
        if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
            if (view.getLayout() == null) {
                // This shouldn't be null, but do something sensible if it is.
                Selection.setSelection(text, text.length());
            }
        } else {
            Selection.setSelection(text, text.length());
        }
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer,
                                MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
            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);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer,
                            buffer.getSpanStart(link[0]),
                            buffer.getSpanEnd(link[0]));
                }
                return true;
            }
        }

        return Touch.onTouchEvent(widget, buffer, event);
    }
}

希望对某人有所帮助。

答案 1 :(得分:14)

我明白了。您需要子类化LinkMovementMethod并添加对文本选择的支持。非常不幸的是,它本身并不支持它。我只是使用source code ArrowKeyMovementMethod中的等效方法来覆盖相关方法。我想这是Android开源的一个好处!

public class CustomMovementMethod extends LinkMovementMethod {
    @Override
    public boolean canSelectArbitrarily () {
        return true;
    }

    @Override
    public void initialize(TextView widget, Spannable text) {
        Selection.setSelection(text, text.length());
    }

    @Override
    public void onTakeFocus(TextView view, Spannable text, int dir) {
       if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
           if (view.getLayout() == null) {
               // This shouldn't be null, but do something sensible if it is.
               Selection.setSelection(text, text.length());
           }
       } else {
           Selection.setSelection(text, text.length());
       }
    }
}

要使用它,只需直接实例化它,如下所示:

textView.setMovementMethod(new CustomMovementMethod());

答案 2 :(得分:2)

LinkMovementMethod()不支持文本选择,即使我们可以选择文本,但在滚动文本视图后,选择将会丢失。

最佳实现是从ArrowKeyMovementMethod扩展,它非常好地支持文本选择。

请参阅here

中的详细信息

答案 3 :(得分:1)

XML TextView不应包含任何可以选择的链接或任何属性:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

然后,按照以下顺序以编程方式设置所有内容:

textView.setText(Html.fromHtml(myHtml));
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setTextIsSelectable(true); // API-11 and above
textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 4 :(得分:0)

TL; DR:只需在此答案末尾使用LinkArrowKeyMovementMethod即可获得完美的解决方案。

如果您尝试使用扩展了LinkMovementMethod的投票最多的答案,就会遇到一个烦人的错误-当您通过单击一些未选中的文本来取消选择时,整个选择会闪烁开始到选择结束,然后什么都没有。这是因为LinkMovementMethod实际上无法处理与ArrowKeyMovementMethod一样好的选择。

如果您将TextView设置为android:autoLink,则可以使用true自己的解决方法,如以下TextView所示:

        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
                getSelectionEnd(), ClickableSpan.class);

            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }

但是我个人不希望使用自动链接功能(我有自己的链接信息),因此基于@Weidian Huang的想法,我将LinkMovementMethod的功能合并到ArrowKeyMovementMethod中,并且建立了一种新的移动方法:

/**
 * @see LinkMovementMethod
 * @see ArrowKeyMovementMethod
 */
public class LinkArrowKeyMovementMethod extends ArrowKeyMovementMethod {

    private static final int CLICK = 1;
    private static final int UP = 2;
    private static final int DOWN = 3;

    private static Object FROM_BELOW = new NoCopySpan.Concrete();

    private static LinkArrowKeyMovementMethod sInstance;

    public static LinkArrowKeyMovementMethod getInstance() {
        if (sInstance == null) {
            sInstance = new LinkArrowKeyMovementMethod();
        }
        return sInstance;
    }

    @Override
    public void initialize(TextView widget, Spannable text) {
        super.initialize(widget, text);

        text.removeSpan(FROM_BELOW);
    }

    @Override
    public void onTakeFocus(TextView view, Spannable text, int dir) {
        super.onTakeFocus(view, text, dir);

        if ((dir & View.FOCUS_BACKWARD) != 0) {
            text.setSpan(FROM_BELOW, 0, 0, Spannable.SPAN_POINT_POINT);
        } else {
            text.removeSpan(FROM_BELOW);
        }
    }

    @Override
    protected boolean handleMovementKey(TextView widget, Spannable buffer, int keyCode,
                                        int movementMetaState, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
                if (KeyEvent.metaStateHasNoModifiers(movementMetaState)) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getRepeatCount() == 0 && action(CLICK, widget, buffer)) {
                        return true;
                    }
                }
                break;
        }
        return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
    }

    @Override
    protected boolean up(TextView widget, Spannable buffer) {
        if (action(UP, widget, buffer)) {
            return true;
        }

        return super.up(widget, buffer);
    }

    @Override
    protected boolean down(TextView widget, Spannable buffer) {
        if (action(DOWN, widget, buffer)) {
            return true;
        }

        return super.down(widget, buffer);
    }

    @Override
    protected boolean left(TextView widget, Spannable buffer) {
        if (action(UP, widget, buffer)) {
            return true;
        }

        return super.left(widget, buffer);
    }

    @Override
    protected boolean right(TextView widget, Spannable buffer) {
        if (action(DOWN, widget, buffer)) {
            return true;
        }

        return super.right(widget, buffer);
    }

    private boolean action(int what, TextView widget, Spannable buffer) {
        Layout layout = widget.getLayout();

        int padding = widget.getTotalPaddingTop() +
                widget.getTotalPaddingBottom();
        int areaTop = widget.getScrollY();
        int areaBot = areaTop + widget.getHeight() - padding;

        int lineTop = layout.getLineForVertical(areaTop);
        int lineBot = layout.getLineForVertical(areaBot);

        int first = layout.getLineStart(lineTop);
        int last = layout.getLineEnd(lineBot);

        ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);

        int a = Selection.getSelectionStart(buffer);
        int b = Selection.getSelectionEnd(buffer);

        int selStart = Math.min(a, b);
        int selEnd = Math.max(a, b);

        if (selStart < 0) {
            if (buffer.getSpanStart(FROM_BELOW) >= 0) {
                selStart = selEnd = buffer.length();
            }
        }

        if (selStart > last)
            selStart = selEnd = Integer.MAX_VALUE;
        if (selEnd < first)
            selStart = selEnd = -1;

        switch (what) {
            case CLICK:
                if (selStart == selEnd) {
                    return false;
                }

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

                if (link.length != 1)
                    return false;

                link[0].onClick(widget);
                break;

            case UP:
                int bestStart, bestEnd;

                bestStart = -1;
                bestEnd = -1;

                for (int i = 0; i < candidates.length; i++) {
                    int end = buffer.getSpanEnd(candidates[i]);

                    if (end < selEnd || selStart == selEnd) {
                        if (end > bestEnd) {
                            bestStart = buffer.getSpanStart(candidates[i]);
                            bestEnd = end;
                        }
                    }
                }

                if (bestStart >= 0) {
                    Selection.setSelection(buffer, bestEnd, bestStart);
                    return true;
                }

                break;

            case DOWN:
                bestStart = Integer.MAX_VALUE;
                bestEnd = Integer.MAX_VALUE;

                for (int i = 0; i < candidates.length; i++) {
                    int start = buffer.getSpanStart(candidates[i]);

                    if (start > selStart || selStart == selEnd) {
                        if (start < bestStart) {
                            bestStart = start;
                            bestEnd = buffer.getSpanEnd(candidates[i]);
                        }
                    }
                }

                if (bestEnd < Integer.MAX_VALUE) {
                    Selection.setSelection(buffer, bestStart, bestEnd);
                    return true;
                }

                break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            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[] links = buffer.getSpans(off, off, ClickableSpan.class);

            if (links.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    links[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer,
                            buffer.getSpanStart(links[0]),
                            buffer.getSpanEnd(links[0]));
                }
                return true;
            }
            // Removed
            //else {
            //    Selection.removeSelection(buffer);
            //}
        }

        return super.onTouchEvent(widget, buffer, event);
    }
}

要使用它,只需调用:

textView.setTextIsSelectable(true);
textView.setMovementMethod(LinkArrowKeyMovementMethod.getInstance());

这对我来说很完美。

答案 5 :(得分:0)

另外,订单很重要

textView.setTextIsSelectable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

允许选择内容,并且链接点击效果完美

答案 6 :(得分:0)

这是我对Kotlin的看法(基于@ hai-zhang的回答)。 简体!见我的要旨以获得更好的版本。我目前将其用于自定义范围,而不是HTML,它对我来说仍然很重要,尤其是当我需要将用户点击的位置传递给范围对象时。

您需要在之后 setTextIsSelectable(true)设置移动方法

/** Minimal version of Smart Movement that only has limited support of [ClickableSpan] */
object SmartMovementMethodMinimal : ArrowKeyMovementMethod() {

    override fun onTouchEvent(widget: TextView?, buffer: Spannable?, event: MotionEvent?) =
        handleMotion(event!!, widget!!, buffer!!) || super.onTouchEvent(widget, buffer, event)

    private fun handleMotion(event: MotionEvent, widget: TextView, buffer: Spannable): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            // Get click position
            val target = Point().apply {
                x = event.x.toInt() - widget.totalPaddingLeft + widget.scrollX
                y = event.y.toInt() - widget.totalPaddingTop + widget.scrollY
            }

            // Get span line and offset
            val line = widget.layout.getLineForVertical(target.y)
            val offset = widget.layout.getOffsetForHorizontal(line, target.x.toFloat())

            if (event.action == MotionEvent.ACTION_UP) {
                val spans = buffer.getSpans<ClickableSpan>(offset, offset)
                if (spans.isNotEmpty()) {
                    spans.forEach { it.onClick(widget) }
                    return true
                }
            }
        }

        return false
    }
}

更详细,更复杂的代码,并在此处提供示例:https://gist.github.com/sQu1rr/210f7e08dd939fa30dcd2209177ba875

答案 7 :(得分:-1)

是否可以将TextView与URL相关联? 您是否有10个TextView和10个URL,编写代码应该很简单,如果单击TextView [3],它会使用URL [3]

触发webview(或浏览器)的意图