Creating a ProgressDialog 创建进度对话框 dialog用法(二)" style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" width="0" height="0"> 一个ProgressDialog(进度对话框)是AlertDialog的扩展。它可以显示一个进度的动画――进度环或者进度条。这个对话框也可以提供按钮,例如取消一个下载等。
  打开一个进度对话框很简单,只需要调用ProgressDialog.show()即可。例如,上图的对话框可以不通过onCreateDialog(int),而直接显示:
  ProgressDialog dialog = ProgressDialog.show(MyActivity.this,"",
  "Loading. Please wait...", true);
  第一个参数是应用程序上下文。第二个为对话框的标题(这里为空),第三个为对话框内容,最后一个为该进度是否为不可确定的(这只跟进度条的创建有关,见下一节)。
  进度对话框的默认样式为一个旋转的环。如果你希望显示进度值,请看下一节。
  Showing a progress bar 显示进度条
  使用一个动画进度条来显示进度:
  使用 ProgressDialog(Context)构造函数来初始化一个ProgressDialog对象。
  将进度样式设置为"STYLE_HORIZONTAL",使用setProgressStyle(int)方法。并且设置其它属性,例如内容等。
  在需要显示时调用show()或者从onCreateDialog(int)回调函数中返回该ProgressDialog。
  你可以使用setProgress(int)或者incrementProgressBy(int)来增加显示的进度。
  例如,你的设置可能像这样:ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
  设置很简单。大部分创建进度对话框需要的代码是在更新它的进程中。你可能需要在一个新的线程中更新它,并使用Handler来将进度报告给Activity。如果你不熟悉使用Handler和另外的线程,请看下列例子,该例子使用了一个新的线程来更新进度。
  Example ProgressDialog with a second thread例--使用一个线程来显示进度对话框
  这个例子使用一个线程来跟踪一个进程的进度(其实为从1数到100)。每当进度更新时,该线程通过Handler给主activity发送一个消息。主Activity更新ProgressDialog.packagecom.example.progressdialog; viewplain
  1. import android.app.Activity;
  2. import android.app.Dialog;
  3. import android.app.ProgressDialog;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class NotificationTest extends Activity {
  11. static final int PROGRESS_DIALOG = 0;
  12. Button button;
  13. ProgressThreadprogressThread;
  14. ProgressDialogprogressDialog;
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. // Setup the button that starts the progressdialog
  19. button = (Button)findViewById(R.id.progressDialog);
  20. button.setOnClickListener(new OnClickListener(){
  21. public void onClick(View v) {
  22. showDialog(PROGRESS_DIALOG);
  23. }
  24. });
  25. }
  26. protected Dialog onCreateDialog(int id) {
  27. switch(id){
  28. case PROGRESS_DIALOG:
  29. progressDialog = new ProgressDialog(NotificationTest.this);
  30. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  31. progressDialog.setMessage("Loading...");
  32. progressThread = new ProgressThread(handler);
  33. progressThread.start();
  34. return progressDialog;
  35. default:
  36. return null;
  37. }
  38. }
  39. // Define the Handler that receives messages fromthe thread and update the progress
  40. final Handler handler = new Handler() {
  41. public void handleMessage(Message msg) {
  42. int total = msg.getData().getInt("total");
  43. progressDialog.setProgress(total);
  44. if (total >= 100){
  45. dismissDialog(PROGRESS_DIALOG);
  46. progressThread.setState(ProgressThread.STATE_DONE);
  47. }
  48. }
  49. };
  50. private class ProgressThread extends Thread {
  51. Handler mHandler;
  52. final static int STATE_DONE = 0;
  53. final static int STATE_RUNNING = 1;
  54. int mState;
  55. int total;
  56. ProgressThread(Handler h){
  57. mHandler = h;
  58. }
  59. public void run() {
  60. mState = STATE_RUNNING;
  61. total = 0;
  62. while (mState == STATE_RUNNING) {
  63. try {
  64. Thread.sleep(100);
  65. } catch (InterruptedException e) {
  66. Log.e("ERROR","Thread Interrupted");
  67. }
  68. Message msg =mHandler.obtainMessage();
  69. Bundle b = new Bundle();
  70. b.putInt("total",total);
  71. msg.setData(b);
  72. mHandler.sendMessage(msg);
  73. total++;
  74. }
  75. }
  76. public void setState(int state) {
  77. mState = state;
  78. }
  79. }
  80. }
Creating a Custom Dialog 创建自定义对话框 dialog用法(二)" style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" width="0" height="0"> 如果你想自定义一个对话框,你可以使用布局元素来创造你的对话框的布局。定义好布局后,将根View对象或者布局资源ID传给setContentView(View).
例如,创建如图所示的对话框:
创建一个xml布局custom_dialog.xml: viewplain
  1. http://schemas.android.com/apk/res/android"
  2. android:id="@+id/layout_root"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:padding="10dp"
  7. >
  8. android:layout_width="wrap_content"
  9. android:layout_height="fill_parent"
  10. android:layout_marginRight="10dp"
  11. />
  12. android:layout_width="wrap_content"
  13. android:layout_height="fill_parent"
  14. android:textColor="#FFF"
  15. />
该xml定义了一个LinearLayout中的一个ImageView 和一个TextView。
将以上布局设为对话框的content view,并且定义ImageView 和 TextView的内容:
viewplain
  1. Context mContext =getApplicationContext();
  2. Dialog dialog = new Dialog(mContext);
  3. dialog.setContentView(R.layout.custom_dialog);
  4. dialog.setTitle("Custom Dialog");
  5. TextView text = (TextView)dialog.findViewById(R.id.text);
  6. text.setText("Hello, this is a custom dialog!");
  7. ImageView image = (ImageView)dialog.findViewById(R.id.image);
  8. image.setImageResource(R.drawable.android);
在初始化Dialog之后,使用setContentView(int),将布局资源id传给它。现在Dialog有一个定义好的布局,你可以使用findViewById(int)来找到该元素的id并修改它的内容。
  使用前面所讲的方法显示对话框。
  一个使用Dialog类建立的对话框必须有一个标题。如果你不调用setTitle(),那么标题区域会保留空白。如果你不希望有一个标题,那么你应该使用AlertDialog类来创建自定义对话框。然而,由于一个AlertDialog使用AlertDialog.Builder类来建立最方便,所以你没有方法使用setContentView(int),而是只能使用setView(View)。该方法接受一个View对象,所以你需要从xml中展开你的根View。
  要展开一个xml布局,使用 getLayoutInflater() (或getSystemService())取得LayoutInflater,然后调用inflate(int,ViewGroup),第一个参数为布局id,而第二个参数为根view的id。现在,你可以使用展开后的布局来找到View对象并定义ImageView和TextView元素的内容。然后实例化AlertDialog.Builder并使用setView(View)来为对话框设置展开后的布局。例如:
viewplain
  1. AlertDialog.Builderbuilder;
  2. AlertDialog alertDialog;
  3. Context mContext =getApplicationContext();
  4. LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  5. View layout =inflater.inflate(R.layout.custom_dialog,
  6. (ViewGroup)findViewById(R.id.layout_root));
  7. TextView text = (TextView)layout.findViewById(R.id.text);
  8. text.setText("Hello, this is a custom dialog!");
  9. ImageView image = (ImageView)layout.findViewById(R.id.image);
  10. image.setImageResource(R.drawable.android);
  11. builder = new AlertDialog.Builder(mContext);
  12. builder.setView(layout);
  13. alertDialog =builder.create();
使用AlertDialog来自定义对话框,可以利用其内置特性例如按钮、选择列表、标题、图标等。

更多相关文章

  1. Android(安卓)Fragment的三种应用方式
  2. Android(安卓)ListView不同的item布局实现
  3. Android四大布局之百分比布局
  4. Android(安卓)布局单位转换
  5. Android之控件保持在软键盘上面
  6. Android在开发中的实用技巧之DialogFragment和AlertDialog(v7包)
  7. android inflater的用法
  8. Android仿饿了么搜索功能
  9. 17. android dialog —— 单选列表对话框

随机推荐

  1. Android(安卓)UI学习总结
  2. xmlns:android="http://schemas.android.
  3. Android应用程序消息处理机制(Looper、Han
  4. 使用VirtualBox在PC上安装Android(安卓)O
  5. Android学习感想一
  6. Android彻底组件化—如何使用Arouter
  7. 为Android加入busybox工具
  8. 【Qt for Android】第一个安卓程序
  9. 【转】Android新手入门 FAQ
  10. Android启动过程——init,Zygote,SystemS