写了个阅读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.(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();
}}