How can I implement scrolling to a specific position in a TextView in Android?
To scroll a TextView to a specific position, you can achieve it by calling the TextView’s scrollTo() or scrollBy() method.
The scrollTo() method scrolls the TextView to a specified coordinate position, with the parameter being relative to the TextView. Here’s an example:
TextView textView = findViewById(R.id.textView);
textView.scrollTo(0, 100); // 将TextView滚动到(0, 100)位置
The scrollBy() method will move the TextView a certain distance relative to its current position, with the parameter being the offset relative to its current position. An example is shown below:
TextView textView = findViewById(R.id.textView);
textView.scrollBy(0, 50); // 将TextView向下滚动50个像素
It is important to note that the TextView must be scrollable in order to achieve the scrolling effect using these two methods. If the content of the TextView is too long, you can set the android:scrollbars=”vertical” attribute in the layout file to make the TextView scrollable.