如何获得在Android的TextView自动向下滚动到最后? [英] How to get the Android TextView to scroll down to the end automatically?

查看:1545
本文介绍了如何获得在Android的TextView自动向下滚动到最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView,其内容从文本文件被复制。现在,该文本文件的内容加载到TextView的每一次,我希望它会自动向下滚动到最后。 这就是那部分我的布局XML文件有:

I have a TextView whose contents are copied from a text file. Now each time the contents of the text file is loaded into the TextView, I want it to scroll down automatically to the end. This is what that portion of my layout XML file has :

    <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>

这是什么功能如下:

And this is what the function looks like :

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);
        }
    });
}

现在我发现了<一个部分解决方案href="http://stackoverflow.com/questions/3087877/scroll-to-last-line-of-tablelayout-within-a-scrollview">here. 因此,对code中的最后一行位,上面写着:

Now I found a partial solution over here. Hence the last line bit of code that says :

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

但是,这仅在第一次的文本文件加载。我怎么总是做的TextView向下滚动到最后?

But this works only the first time the text file is loaded. How do I always make the TextView scroll down to the end?

推荐答案

编辑: 使用这样的:

    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);
            }
        }
    });

这篇关于如何获得在Android的TextView自动向下滚动到最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆