控制台TextView Android

Fri*_*n3L 5 android scrollview textview

我是android开发的新手。我正在做的应用程序将经历一些过程,并且在此过程中会发生一些事情。因此,我希望活动屏幕中的可视控制台可以显示正在发生的事情。我的想法是在textview中插入行,并且必须始终在最后一行中刷新它并能够滚动到开头。

如何在代码中执行此操作?

谢谢

Gui*_*ume 6

如果您有一个TextView具有固定大小的简单对象ScrollView,则它在内的行为将完全像您想要的那样。您唯一要做的就是确保您不会每次都覆盖现有文本,而是将其附加到末尾。保留对的实际内容的引用TextView(一个简单的String方法即可),并进行相应的更新,然后使用myTextView.setText(newValue)

将文字设置为时TextView,视图将自动刷新。

<ScrollView android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1">
    <TextView android:id="@+id/textView" 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

在您的活动中,当您想添加newText到视图中时:

this.currentText += "\n" + newText;
final TextView myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText(currentText);
Run Code Online (Sandbox Code Playgroud)

最后我还建议向下滚动到最后一行:电话fullScroll(View.FOCUS_DOWN)ScrollView