综合作业1:Android文本阅读器

   

    文本阅读器,区别于小说阅读器,不能读取大容量的文件。文件容量过大,就会导致读取的速度变得很慢。

    读取一个文本文件需要使用BufferedReader + FileReader来逐行读取内容。
还需要准备一个滚动面板配合TextView来显示内容。

如果文件比较大,读取时间会比较长,就需要使用ProgressDialog来建立进度条,提示用户当前正在加载数据。

 

首先,我们看下整体的实现效果:

 

注:这里设置只显示了SD卡中的文件夹以及文本文件,这是通过扩展名的判断实现的

 

实行步骤:第一步:

一.创建activity_main布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="当前位置: /mnt/sdcard"
        android:textSize="14sp" />


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:cacheColorHint="#00000000" >
    </ListView>


</LinearLayout>

 

二、创建给列表添加数据的file_line.xml布局:两个TextView,一个是显示文件夹图片的,一个事显示文件夹名字的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/file_img"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />


    <TextView
        android:id="@+id/file_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:textSize="14sp" />


</LinearLayout>

 

三、创建显示文本的activity

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff" >
<TextView
        android:id="@+id/detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="14sp" />
</ScrollView>

 

四、布局写完后,下面是老样子,先编写一个Globals的公共类。
用来动态计算手机的高度和宽度,等比放大缩小。还有初始化所有扩展名和图片的对应关系

import java.util.HashMap;
import java.util.Map;
import org.liky.txt.R;
import android.app.Activity;
public class Globals {
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
// 建立一个Map集合, 里面封装了所有扩展名对应的图标图片, 以便进行文件图标的显示
public static Map<String, Integer> allIconImgs = new HashMap<String, Integer>();
public static void init(Activity a) {
SCREEN_WIDTH = a.getWindowManager().getDefaultDisplay().getWidth();
SCREEN_HEIGHT = a.getWindowManager().getDefaultDisplay().getHeight();
// 初始化所有扩展名和图片的对应关系
allIconImgs.put("txt", R.drawable.txt_file);
allIconImgs.put("mp3", R.drawable.mp3_file);
allIconImgs.put("mp4", R.drawable.mp4_file);
allIconImgs.put("bmp", R.drawable.image_file);
allIconImgs.put("gif", R.drawable.image_file);
allIconImgs.put("png", R.drawable.image_file);
allIconImgs.put("jpg", R.drawable.image_file);
allIconImgs.put("dir_open", R.drawable.open_dir);
allIconImgs.put("dir_close", R.drawable.close_dir);
}


}

五、创建一个自定义FileAdapter,用于读取,显示界面的列表中的数据,包括通过扩展名取得的图片和文件名:

import java.util.ArrayList;

import java.util.List;
import java.util.Map;
import org.liky.txt.R;
import org.liky.txt.util.Globals;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class FileAdapter extends BaseAdapter {
private Context ctx;
private List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();
public FileAdapter(Con

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值