我有一个可滚动的TextView
用户可以在其中选择文本。我通过将移动方法设置为来添加滚动条ScrollingMovementMethod
.
Problem:除非应用程序暂停(例如,切换应用程序后),否则选择效果很好。一旦应用程序再次处于活动状态,选择就会停止工作,我会在日志中收到以下消息:
W/TextView:TextView不支持文本选择。选择
取消。
我的设置:
我有一个活动CoordinatorLayout
和一个带有TextView
包裹成RelativeLayout
看起来像这样:
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:scrollbars="vertical" />
在 Java 代码中我必须这样做:
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTextIsSelectable(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
因为根据规定,这是唯一的工作方式this https://stackoverflow.com/q/15857755/2065796, this https://stackoverflow.com/q/37566303/2065796 and this https://issuetracker.google.com/issues/37095917 issues.
EDIT:
问题出在下面的调用中
textView.setMovementMethod(new ScrollingMovementMethod());
如果我删除它,它会起作用,但我不明白为什么。
重现该问题的最少步骤:
1)创建一个空的ActivityTextView
使用以下布局。
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:text="Some very very very long text..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:scrollbars="vertical" />
</android.support.design.widget.CoordinatorLayout>
2)设置可见性参数TextView
in onStart()
方法。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
TextView textView = findViewById(R.id.text_view);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTextIsSelectable(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
}
}
3)尝试使用上下文菜单TextView
暂停应用程序之前和之后。
EDIT 2:
去除setMovementMethod(new ScrollingMovementMethod())
解决了我的问题,之后功能运行良好。但我不太清楚为什么要添加它,而且我担心如果删除它,它会破坏某些东西。知道为什么有人会使用ScrollingMovementMethod
结合android:scrollbars="vertical"
。也许 xml 在某些情况下不起作用?有想法吗?我仍然感兴趣为什么使用ScrollingMovementMethod
刹车选择功能?