android TextView显示容量很大的.txt文件

son123qaz123 2012-01-11 10:46:45
写了个阅读txt文件的小程序显示数据时候数据太多导致UI线程阻塞,界面卡住,有没有方法分段的读取数据并且在TextView显示啊求高手解决啊!
代码
public class TextActivity extends Activity {

private TextView text_title;
private DigitalClock clock;
private TextView myView;// 文本
private ImageView menu_button;// 菜单按钮
private ImageView close_button;// 取消按钮
private String textPath;// 获取文本文件的路径
private ProgressDialog progressDialog;
private StringBuffer stringBuffer;// 存放加载的数据
private static final int finsh_id = 1;
private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Mini文件阅读器");
setContentView(R.layout.text_rw);
text_title = (TextView) findViewById(R.id.text_title);
clock = (DigitalClock) findViewById(R.id.clock);
myView = (TextView) findViewById(R.id.myView);

menu_button = (ImageView) findViewById(R.id.menu_button);
menu_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(TextActivity.this,
MenuActivity.class);
startActivity(intent);
}
});
close_button = (ImageView) findViewById(R.id.close_button);
close_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
TextActivity.this.finish();
}
});
textPath = getIntent().getExtras().getString("textPath");

String text_name = textPath.substring(textPath.lastIndexOf("/") + 1,
textPath.lastIndexOf("."));// 获得文件名称,不包含扩展名称
text_title.setText(text_name);

HandlerThread handlerThread = new HandlerThread("handlerThread");// 创建异步对象
handlerThread.start();
Handler threadHandler = new Handler(handlerThread.getLooper()) {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
convertCodeAndGetText(textPath);// 加载数据并且转码
handler.sendEmptyMessage(finsh_id);
}

};
Message message = threadHandler.obtainMessage();
message.sendToTarget();// 启动异步对象

handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == finsh_id) {
progressDialog.dismiss();
myView.setText(stringBuffer.toString());//显示数据
}

}

};

progressDialog = ProgressDialog.show(this, "请稍后...", "加载数据中...", false);
}

public void convertCodeAndGetText(String path) {// 转码

stringBuffer = new StringBuffer();

File file = new File(path);

BufferedReader reader;
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);// 找到文档的前三个字节并自动判断文档类型。
in.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(
new InputStreamReader(in, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
}

String str;

while ((str = reader.readLine()) != null) {
stringBuffer.append(str + "\n");
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
}

}
...全文
815 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
木糖醇的爸爸 2012-01-28
  • 打赏
  • 举报
回复
阅读器的textView是不用全部把文本读进去的 一般情况下是预读当前页的后几页 然后翻一页读一页。。一下子读完完全就是很容易卡住死机的而且耗内存 具体算法自己实现。。。
bcf102 2012-01-21
  • 打赏
  • 举报
回复
RandomAccessFile
用这个读就行了,具体用法自己查
源码链接: https://pan.quark.cn/s/651b74ed6067 《MQL4中文帮助手册》是一部详尽阐述MetaQuotes Language 4(MQL4)编程语言的参考书籍,其核心目标在于协助用户掌握并运用MQL4进行金融市场的自动化交易系统构建。MQL4作为MetaTrader 4交易平台的核心编程语言,赋予了交易者开发自定义指标、Expert Advisors(EA智能交易系统)以及脚本的能力。 ### MQL4基础知识 1. **Syntax 语法**:MQL4的语法规则与C/C++语言展现出相似性,涵盖了变量声明、函数调用、条件判断、循环机制等核心要素。熟练掌握并合理运用语法是开发高效MQL4代码的先决条件。 2. **Comments 注释**:在MQL4编程环境中,单行注释采用两个斜线(//)进行标识,而多行注释则通过/* 和 */进行界定。注释的主要功能在于阐释代码用途,从而提升代码的可读性与可维护性。 3. **Identifiers 标识符**:标识符是程序中用于命名变量、函数、类等元素的字符序列。其命名规则要求必须以字母或下划线作为开头,后续可包含字母、数字或下划线组合。MQL4语言对大小写敏感,因此`MyVariable`与`myvariable`被视为两个独立的标识符。 4. **Reserved words 保留字**:保留字是MQL4语言中预先设定的特殊词汇,例如`int`, `bool`, `double`, `if`, `else`, `for`, `while`等,这些词汇不可用作自定义标识符,以防止与语言关键字产生冲突。 5. **Data types 数据类型**:MQL4支持多元化的数据类型,包括...

80,489

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧