4

我有一些代码使用带有链接移动方法的文本视图,以便可以单击单个单词。

但是,我真正需要的是滚动运动方法。当我这样做时,单个跨越的段不再是可点击的。

不幸的是,这意味着它使用垂直滚动,即使android:scrollHorizontally="true"指定了水平滚动 ( )。

我试图弄清楚如何保留在单个跨词上捕获 onClick 的能力,并且仍然允许它垂直滚动。

有人可以提供有关如何两全其美的建议吗?

    String page = getPage();
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(page, BufferType.SPANNABLE);


    Spannable ssPage = (Spannable) textView.getText();
    Integer[] indices = getSpaceIndices(textView.getText().toString(), ' ');

    int start = 0;
    int end = 0;
      // to cater last/only word loop will run equal to the length of indices.length
    for (int i = 0; i <= indices.length; i++) {
        ClickableSpan clickSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                TextView tv = (TextView) widget;
                String s = tv
                        .getText()
                        .subSequence(tv.getSelectionStart(),
                                tv.getSelectionEnd()).toString();
                Log.d("called", s);
                Speak (s);

                //textView.scrollBy(tv.getWidth(), tv.getHeight());
            }

            public void updateDrawState(TextPaint ds) {
                 super.updateDrawState(ds);
                 ds.setUnderlineText(false);
            }
        };
       // to cater last/only word
        end = (i < indices.length ? indices[i] : ssPage.length());
        ssPage.setSpan(clickSpan, start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        start = end + 1;
    }

布局:

    <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:alpha="245"
    android:ellipsize="end"
    android:gravity="top"
    android:paddingBottom="15dip"
    android:scrollHorizontally="true"

    android:scrollbars="horizontal"
    android:text="@string/hello_world"
    android:textColorLink="@android:color/black"
    android:textSize="20dip"
    tools:context=".ReadActivity" />
4

1 回答 1

-1

LinkMovementMethod已经“遍历文本缓冲区中的链接并在必要时滚动”。所以如果你想要可点击的链接和可滚动的视图,你可以使用它。

然后你必须像这样调整 textview 的布局:

    android:maxLines="3"
    android:scrollbars="vertical"

通过设置maxLines,它后面的多余行将超过高度,从而产生一个可滚动的文本视图。

于 2013-10-25T03:01:20.040 回答