ProgressDialog必须要在[color=red]后台程序[/color]运行完毕前,以dismiss()方法来关闭缺的焦点的对话框,否则程序会陷入无法终止的无穷循环中;第二,在后台线程处理中不可有任何更改Context或parent View的任何状态,文字输出等事件,因为线程里的Context与View并不属于parent,两者也没有任何关联,因为这时候parent View已经失去焦点,焦点是在ProgressDialog上面,所以你修改parent View的任何状态都会报错。
简单的小例子:
简单的小例子:
package com.kevin.progressDialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener{
private Button btn_show;
private TextView title;
private ProgressDialog progress;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_show = (Button) findViewById(R.id.button1);
title = (TextView) findViewById(R.id.title);
btn_show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
CharSequence progressTitle = getString(R.string.progressTitle);
CharSequence progressBody = getString(R.string.progressBody);
// 显示ProgressDialog
progress = ProgressDialog.show(this, progressTitle, progressBody);
title.setText(progressBody);
new Thread(new MyThread()).start();
}
class MyThread implements Runnable{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
// 关闭ProgressDialog
progress.dismiss();
/*
* 错误的,在线程里不可有任何更改Context或者parent View的任何
* 状态,文字输出等事件,因为县城里的Context和View并不属于parent,
* 两者之间也没有关联。
*/
//title.setText(R.string.finshed);
}
}
}
}