您的位置:首页 > 其它

ProgressDialog(1)

2016-01-27 16:45 204 查看

ProgressDialog(1)

在Android中程序的“加载中”对话框,是通过ProgressDialog来运行,这个类封装在Android.app.ProgressDialog中,但是需要留意的是Android中的ProgressDialog必须要在后台程序运行完毕前,以dismmiss()方法来关闭取得焦点(focus)的对话框,否则程序就会陷入无法终止的无穷循环中;又或者在线程里不可能有任何更改Context或parent View的任何状态、文字输出等事件,因为线程里的Context与View并不属于parent,两者之间没有关联。所以在下面我们以线程(Thread)来模拟后台程序的运行,在通过线程运行完毕这个加载中的动画对话框。

以下的程序示范将设计一个Button,在单机按钮之后开始线程的周期,在运行的过程中显示ProgressDialog,最后当线程运行完毕后选择结束ProgressDialog对话框

public class MyActivity extends Activity {

/**

* Called when the activity is first created.

*/

private Button button;

private TextView textView;

public ProgressDialog progressDialog = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(myShowProgressBar);
}
Button.OnClickListener myShowProgressBar = new Button.OnClickListener(){

@Override
public void onClick(View view) {
final CharSequence strDialogTitle = getString(R.string.str_dialog_title);
final CharSequence strDialogBody = getString(R.string.str_dialog_body);
//显示ProgressDialog对话框
progressDialog = ProgressDialog.show(MyActivity.this,strDialogTitle,strDialogBody,true);
textView.setText(R.string.str_dialog_title);
new Thread(){
@Override
public void run() {


// super.run();

try{

sleep(3*1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

finally{

progressDialog.dismiss();

}

}

}.start();/开始运行线程/

}/End:public void onClick(View arg0)/

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: