textview 滚动显示.txt

来源:互联网 发布:21lic单片机项目外包网 编辑:程序博客网 时间:2024/06/01 10:07


读取文件类

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;public class TxtReader {    /**     * 通过一个InputStream获取内容     *     * @param inputStream     * @return     */    public static String getString(InputStream inputStream) {        InputStreamReader inputStreamReader = null;        try {            inputStreamReader = new InputStreamReader(inputStream, "utf-8");        } catch (UnsupportedEncodingException e1) {            e1.printStackTrace();        }        BufferedReader reader = new BufferedReader(inputStreamReader);        StringBuffer sb = new StringBuffer("");        String line;        try {            while ((line = reader.readLine()) != null) {                sb.append(line);                sb.append("\n");            }        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }    /**     * 通过txt文件的路径获取其内容     *     * @param filepath     * @return     */    public static String getString(String filepath) {        File file = new File(filepath);        FileInputStream fileInputStream = null;        try {            fileInputStream = new FileInputStream(file);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        return getString(fileInputStream);    }}
布局 就一个textview

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:showIn="@layout/activity_main" tools:context=".MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text=""        android:id="@+id/textView"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentBottom="true" /></RelativeLayout>

读取文件 与滚动设置

InputStream inputStream = getResources().openRawResource(R.raw.q);        String string = TxtReader.getString(inputStream);        TextView textView=(TextView)findViewById(R.id.textView);        textView.setText(string);        textView.setMovementMethod(ScrollingMovementMethod.getInstance());




0 0
原创粉丝点击