综合作业2:Android文本阅读器源代码

来源:互联网 发布:编程的本质 pdf 网盘 编辑:程序博客网 时间:2024/06/07 02:32
<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>

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

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

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


}


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(Context ctx, List<Map<String, Object>> allValues) {
this.ctx = ctx;
this.allValues = allValues;
}


@Override
public int getCount() {
return allValues.size();
}


@Override
public Object getItem(int arg0) {
return allValues.get(arg0);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.file_line,
null);
// 设置高度
convertView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, Globals.SCREEN_HEIGHT / 9));
}


// 取得组件
TextView fileImg = (TextView) convertView.findViewById(R.id.file_img);


fileImg.getLayoutParams().height = Globals.SCREEN_HEIGHT / 9;


TextView fileName = (TextView) convertView.findViewById(R.id.file_name);


// 取得数据,设置到组件里
Map<String, Object> map = allValues.get(position);


// 设置内容, 文字
fileName.setText(map.get("fileName").toString());


// 图片要根据扩展名取得
String extName = map.get("extName").toString();
// 取得图片的id
int imgId = Globals.allIconImgs.get(extName);
// 设置图片
fileImg.setBackgroundResource(imgId);


return convertView;
}


}
package org.liky.txt;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;


public class DetailActivity extends Activity {


private TextView detail;


// 声明这个Handler类
private Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);


detail = (TextView) findViewById(R.id.detail);


// 建立这个handler对象,并覆写handleMessage方法,该方法会在接收到子线程的消息时自动执行,进行界面修改
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// msg就是子线程发送过来的消息
// 修改text内容
detail.setText(msg.obj.toString());
}
};


// 建立进度条
final ProgressDialog dialog = new ProgressDialog(this);
// 设置为水平进度条
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setTitle("提示");
dialog.setMessage("正在加载数据, 请稍候...");
// 设置最大值
File f = new File(getIntent().getStringExtra("fullPath"));
dialog.setMax((int) f.length());


dialog.show();


Thread t = new Thread() {
@Override
public void run() {


// 接收参数,并根据参数建立文件对象
File f = new File(getIntent().getStringExtra("fullPath"));
// 使用IO流读取
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(f), "GBK"));
String line = null;
StringBuilder builder = new StringBuilder();


// 定义保存读取内容大小的变量
int size = 0;


while ((line = reader.readLine()) != null) {
builder.append(line + "\r\n");
// 当读取了一部分内容后,根据读取内容的大小,然后增长进度
size += line.getBytes().length;
// 每增加超过10k的内容,就改变一次进度条
if (size >= 10240) {
dialog.incrementProgressBy(size);
size = 0;
// 睡眠一段时间
Thread.sleep(5);
}
}


reader.close();


// detail.setText(builder.toString());
Message msg = new Message();
// 将builder中的内容设置到msg里
msg.obj = builder;
// 发送消息
handler.sendMessage(msg);


// 关闭进度条
dialog.dismiss();


} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();


}


}
0 0
原创粉丝点击