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)/
};
}
相关文章推荐
- 关于build.gradle的写法
- int long long
- Linux crontab 定时任务详解
- js\java 通过算法加密后,再base64
- GPS转百度,百度近似转换GPS
- 由一次long SQL调优引发de思考
- 2-Metasploit基础
- [从头学数学] 第65节 四则运算
- Apple 企业开发者账号&邓白氏码申请记录
- Thrift的TCompactProtocol紧凑型二进制协议分析
- css漂亮表格样式
- 统计数组中每个元素出现的次数
- 设计模式--14、中介者模式
- Array 数组常用方法
- 连续字符换行 溢出点点点 多行省略
- 如何搭建文件服务器
- 平衡二叉树---Balanced Binary Tree
- JAVA 性能优化
- Android 6.0 Marshmallow SettingsProvider
- php的strlen和mb_strlen的区别