【问题标题】:Clickable span in TextView problems with ScrollViewScrollView 的 TextView 问题中的可点击范围
【发布时间】:2022-01-30 05:14:39
【问题描述】:

我有一个带有可点击单词列表的 textview 片段。它包含在相对布局中的文本视图可以使用滚动视图滚动。 如果我使用滚动视图,则很难处理可点击跨度上的点击事件,但如果我删除滚动视图,点击事件将正常工作。

这是布局代码

<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/scroll1"
            android:scrollbars="vertical"
            android:fadingEdge="vertical">
    <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:padding="40dp">
    <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:id="@+id/sinonimiContainer"
                android:background="@drawable/layerlist"
                android:layout_centerHorizontal="true">
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
                  android:id="@+id/titolo"
                  android:text="@string/titoloSinonimi"
                  android:textSize="60sp"
                  android:paddingBottom="25dp"
                  android:paddingTop="30dp"
                  android:paddingLeft="50dp"
                  android:textColor="@color/nero"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:id="@+id/txtSinonimi"
                    android:textColor="@color/nero"
                    android:textSize="27sp"
                    android:paddingBottom="30dp"
                    android:paddingLeft="30dp"
                    android:paddingRight="30dp"
                    android:clickable="true"
                    android:layout_below="@id/titolo" />

    </RelativeLayout>
    </RelativeLayout>
</ScrollView>

而这个是插入到textview中的可点击单词(txtsinonimi)

private void setParole(String parole)
{
    parole = parole.trim();
    Spannable spans;
    txtSinonimi.setText(parole, TextView.BufferType.SPANNABLE);
    spans = (Spannable) txtSinonimi.getText();

    String[] fields = parole.split(", ");

    for (String word: fields)
    {
        word = word.trim();
        if ( Character.isLetterOrDigit(word.charAt(0))) {
            ClickableSpan clickSpan = getClickableSpan(word);
            int index = parole.indexOf(word);
            spans.setSpan(clickSpan, index, index + word.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    txtSinonimi.setMovementMethod(LinkMovementMethod.getInstance());
}

/* evento click sulla parole/link */
private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }

        @Override
        public void onClick(View widget)
        {
            Intent intent = new Intent(getActivity(), SearchResult.class);
            intent.putExtra(SearchManager.QUERY, mWord);
            startActivity(intent);
        }
    };
}

如何在滚动视图内使用带有可点击跨度的文本视图??

【问题讨论】:

  • “很难”是什么意思?
  • 看看这个帖子stackoverflow.com/questions/4061118/…,也许这可以帮助你更好地理解你的问题,你不会在那里找到解决方案......
  • 这很难意味着它每尝试 4 次就工作一次,并且它在 textview 的底部正常工作,但在顶部没有
  • 尝试使用 TextView 作为 ScrollView 的唯一子项来重现它,看看它是否工作正常
  • @pskink 现在可以正常工作,但我需要 textview 周围的相对布局。

标签: android android-layout android-scrollview


【解决方案1】:

我遇到了类似的问题。我在 ScrollView 中有一个 TextView,并且 ClickableSpan “附加”在那个 TextView 上。 ClickableSpan 在单击时响应,但不会在单击时突出显示 TextView 的部分。我已经通过使用解决了它:

android:focusableInTouchMode="true"

在 TextView 中。

【讨论】:

    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 2011-11-23
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多