当textIsSelectable设置为true时,Android TextView无法更新其文本

5
我遇到了一个奇怪的问题,让我很头疼和麻烦。
我发布了一个包含多本书的应用程序,我决定使用TextView来显示每一章的文本,因为它符合要求,而且使用起来快速简便。
用户要求能够选择并复制文本,我使用了textIsSelectable = true,一切都很好,第二天他打电话告诉我文本没有改变,你打开一章然后切换到另一章,但是文本仍然是相同的。
我以为可能是我的逻辑问题,但经过一些调试,问题出在TextView上,当我设置textIsSelectable = false时,一切正常。
为了确保,我使用了一个静态int,在每次显示文本时增加,但文本仍然没有改变。
那么问题是什么?我是否错误地使用了TextView?还是TextView本身存在某些bug?
这是我的布局(txtBody是导致问题的TextView)。
        <?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/white">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:fillViewport="true"
            >

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical"
                android:gravity="right">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title Text"
                    android:id="@+id/txtTitle"
                    android:textSize="24sp"
                    android:padding="5dp"
                    android:gravity="right"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Detail Text"
                    android:id="@+id/txtBody"
                    android:background="@android:color/white"
                    android:gravity="right"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:padding="5dp"
                    android:enabled="true"
                    android:textIsSelectable="true"
                    android:selectAllOnFocus="false"
                    android:soundEffectsEnabled="true"/>

        </LinearLayout>


    </ScrollView>


</FrameLayout>

我使用以下代码来设置文本:

String body = MainDataManager.getInstance().getChapterBody(book.getTable(), chapter.getId());
    updateTextStyle();
    txtTitle.setText(chapter.getTitle());
    txtBody.setText(body, TextView.BufferType.SPANNABLE);

经过2~3个小时的尝试,我发现这就是原因。我讨厌这样浪费我的时间...对于我的谷歌搜索,这个页面是第一个命中的,第二个是[https://code.google.com/p/android/issues/detail?id=78491]。描述与我的情况完全相符。它在2014年被标记为一个中等优先级的BUG,并且到了2016年仍未被修复。这不是我第一次因为一个几年都没有被修复的bug而浪费时间了... - Damn Vegetables
1个回答

0
在更改TextView的文本之前,请使用invalidate()方法。
 String body = MainDataManager.getInstance().getChapterBody(book.getTable(), chapter.getId());
    updateTextStyle();
    txtTitle.invalidate();
    txtTitle.setText(chapter.getTitle());
    txtBody.invalidate();
    txtBody.setText(body, TextView.BufferType.SPANNABLE);

然而,我不得不在活动的OnStart()或OnResume()方法中执行invalidate。 - Kumait

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接