如何让Android TextView自动向下滚动到末尾?

3

我有一个TextView,其内容是从文本文件中复制的。现在每次将文本文件的内容加载到TextView中时,我希望它自动向下滚动到末尾。 以下是我的布局XML文件中的部分内容:

    <ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/command"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:fillViewport="true" >

        <TextView
            android:id="@+id/output"
            android:layout_width="fill_parent"
            android:layout_height="132dp"
            android:bufferType="spannable"
            android:editable="false"
            android:enabled="false"
            android:focusable="true"
            android:focusableInTouchMode="false"
            android:freezesText="true"
            android:inputType="textMultiLine"
            android:isScrollContainer="true"
            android:scrollbars="vertical"
            android:text="@string/output" >

            <requestFocus />
        </TextView>
    </ScrollView>

这就是函数的样子:

public void displayOutput()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/Android/data/terminalemulatorlog.txt");
    StringBuilder text = new StringBuilder();
    try 
    {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) 
        {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) 
    {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    }
    TextView output=(TextView) findViewById(R.id.output);
    output.setText(text);
    ((ScrollView) findViewById(R.id.scroller)).post(new Runnable() 
    {
        public void run() 
        {
            ((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);
        }
    });
}

现在我在这里找到了一个部分解决方案。因此,最后一行代码如下:

((ScrollView) findViewById(R.id.scroller)).post(new Runnable() 
{
    public void run() 
    {
        ((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);
    }
});

但是这只在文本文件第一次加载时有效。我该如何始终使TextView滚动到末尾?


当文本加载完成时,您可以自动滚动到底部。这可以在以下三种情况下实现:1)当文本加载完成时,2)当活动首次显示时(onCreate()),或者3)每次活动显示时(onPrepare())。问题:这符合您的要求吗? - undefined
第一点。我希望在文本加载完成后,它能自动滚动到底部。onCreate()方法无法实现这个功能。而且我也不确定onPrepare()方法的作用是什么。 - undefined
2个回答

9

只需要在你的TextView中添加以下属性:
android:gravity="bottom" android:scrollbars = "vertical"

并且设置移动方法:
myTextView.setMovementMethod(new ScrollingMovementMethod());


注意:请保留原有的html标签,不做任何修改。

2
欢迎来到SO!顺便提一下,使用反引号(`)将文本括起来可以格式化单行代码。如果你想换行的话,在上一行结束后加两个空格即可。 - undefined

7

编辑: 使用以下内容:

    final ScrollView scroller = (ScrollView) findViewById(R.id.scroller);
    scroller.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                scroller.fullScroll(View.FOCUS_DOWN);
            }
        }
    });

当我使用以下代码片段时,应用程序崩溃了 :( ScrollView output = (ScrollView) findViewById(R.id.output); output.setOnFocusChangeListener(scrolldown); - undefined
输出向下滚动,但不完全。我在这下面有一个EditText框。可能是因为键盘几乎总是覆盖在TextView上,所以才会出现问题吗?我希望它像短信应用程序一样,在键盘的状态(最大化/隐藏)无论如何,每次收到新的短信时都会向下滚动。 - undefined
3
由于缺乏适当的描述,此问题被投下了负票。它只是一段代码而已。 - undefined

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