ProgressDialog

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);
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值