添加android:textIsSelectable会导致TextView自动滚动。

5
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/solutions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textIsSelectable="true"
            android:textSize="18sp"
            tools:text="1.  1+2+3 = 6"/>
    </HorizontalScrollView>
</ScrollView>

TextView中添加android:textIsSelectable="true"后,TextView将自动滚动到底部。然而,我不想要这种行为。
2个回答

0
对于遇到同样问题的人:您有两个滚动视图,您可能需要稍微调整一下或使用NestedScrollView。最终,这对我起作用了:
在我的TextView的父级上使用以下代码:

android:descendantFocusability="afterDescendants

请尝试此方法:
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
       android:descendantFocusability="afterDescendants"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/solutions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textIsSelectable="true"
            android:textSize="18sp"
            tools:text="1.  1+2+3 = 6"/>
    </HorizontalScrollView> </ScrollView>

更新:尽管这似乎有效,但实际上并不是。视图会随机跳动,因此在我的测试中一开始看起来很好。

我找不到一个合适的解决方案,所以我使用了一些不太正规的方法。我的文本视图现在是“不可选择的”。在我的Fragment的onCreateView()中,我在一个新线程中等待1.5秒,并在Fragment视图完全构建后从UI线程更新文本视图。这样,文本视图就不会重新聚焦,滚动视图也不会跳来跳去:

        Runnable x = () -> {
            try {
                Thread.sleep(1500);
                if(getActivity()!=null){

                    getActivity().runOnUiThread(() -> {
                        textview.setFocusable(true);
                       textview.setFocusableInTouchMode(true);
                        textview.setTextIsSelectable(true);
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        new Thread(x).start();

-1
尝试这个:添加。
android:focusable="false"
android:focusableInTouchMode="false"

 <TextView
        android:id="@+id/solutions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:lineSpacingMultiplier="1.2"
        android:text="1.  1+2+3 = 6"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textIsSelectable="true"
        android:textSize="18sp" />

解决方案的原因是:
当您使用 android:textIsSelectable="true" 时,实际上是在调用 TextView.setTextIsSelectable(true),这会调用 View.setFocusableView.setFocusableInTouchMode,并导致 scrollview 自动滚动文本。

4
这使得文本无法被选择。 - Andre
请告诉我们更多关于您的想法。 - Felix D.
虽然这段代码可能回答了问题,但提供有关它如何以及为什么解决问题的附加上下文将改善答案的长期价值。请记住,您正在为未来的读者回答问题,而不仅仅是现在提问的人!请编辑您的答案以添加说明,并指出适用的限制和假设。 - Dev-iL
4
我确认这将使得在API 23上的文本无法被选择,因此它是无用的。 - mhsmith
结果Logcat:TextView W TextView不支持文本选择。选择已取消。 - CaptainCrunch

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