android progressdialog 对话框试用实例
ProgressDialog 跟AlertDialog用法差不多,不同的是:ProgressDialog 显示的是一种"加载中"的效果,android 中 ProgressDialog 必须要在后台程序运行完毕前 使用dismiss()方法来关闭取得焦点(focus)的对话框,否则程序会陷入死循环.
试用:
package com.app.progressdialogtest; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.Window; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ProgressDialog dialog = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1、不能将Activity布局设计为无标题,并在setContentView()之前设置一下标题布局形式: requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); // setProgressBarIndeterminateVisibility(true); dialog = new ProgressDialog(this); // dialog = // ProgressDialog.show(getApplicationContext(),"请等待","网络加载中...",false); // dialog.setTitle("请等待.."); dialog.setMessage("网络给力加载中..."); // dialog.setIndeterminate(indeterminate); dialog.setCancelable(false); // dialog.setOnCancelListener(cancelListener); dialog.show(); new Thread(new Runnable() { @Override public void run() { try { getConnetction(); Message msg = new Message(); handler.sendMessage(msg); // setProgressBarIndeterminateVisibility(false); } catch (Exception e) { e.printStackTrace(); } } }).start(); } Handler handler = new Handler() { public void handleMessage(Message msg) { // getConnetction(); imageView.setImageBitmap(bitmap); dialog.dismiss(); }; }; Bitmap bitmap; ImageView imageView; private void getConnetction() { String path = "http://www.baidu.com/img/baidu_jgylogo3.gif"; Log.d("xxxxxx", "-"); try { URL url = new URL(path); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); imageView = (ImageView) findViewById(R.id.imageView1); // imageView.setImageBitmap(bitmap); } catch (MalformedURLException e) { Toast.makeText(getApplicationContext(), path + "error", 0).show(); e.printStackTrace(); } catch (IOException e) { Toast.makeText(getApplicationContext(), path + "connection error", 0).show(); e.printStackTrace(); } } }
简单应用:
在联网获取数据时,用一个进度条提示用户正在加载数据,。由于Android的界面更新只能通过自己的UI线程进行操作,所以我们需要用到Handler在进行更新界面的操作。
1、声明变量
private Handler handler = new Handler();
private ProgressDialog progressDialog = null;
2、在开始进行网络连接时显示进度条对话框
progressDialog = ProgressDialog.show(MyActivity.this, "请稍等...", "获取数据中...", true);
3、在handler.post中进行界面更新
public void setListAdapter(){
handler.post(new Runnable() {
public void run() {
//将获取到的数据更新到列表中
MyListView.setAdapter(saImageItems);
}
}
});
4、开个新的线程进行网络连接获取数据
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//向服务器请求数据
mapList=MyAPI.getAllDatas();
setListAdapter(mapList);
//更新完列表数据,则关闭对话框
progressDialog.dismiss();
}}).start();
5.整个过程就这样了