进度条dialog和自定义dialog

Creating a ProgressDialog 创建进度对话框
一个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.package com.example.progressdialog;
[java]  view plain copy
  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.     ProgressThread progressThread;  
  14.     ProgressDialog progressDialog;  
  15.     /** Called when the activity is first created. */  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);   
  19.         // Setup the button that starts the progress dialog  
  20.         button = (Button) findViewById(R.id.progressDialog);  
  21.         button.setOnClickListener(new OnClickListener(){  
  22.             public void onClick(View v) {  
  23.                 showDialog(PROGRESS_DIALOG);  
  24.             }  
  25.         });  
  26.     }  
  27.     protected Dialog onCreateDialog(int id) {  
  28.         switch(id) {  
  29.         case PROGRESS_DIALOG:  
  30.             progressDialog = new ProgressDialog(NotificationTest.this);  
  31.             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  32.             progressDialog.setMessage("Loading...");  
  33.             progressThread = new ProgressThread(handler);  
  34.             progressThread.start();  
  35.             return progressDialog;  
  36.         default:  
  37.             return null;  
  38.         }  
  39.     }   
  40.     // Define the Handler that receives messages from the thread and update the progress  
  41.     final Handler handler = new Handler() {  
  42.         public void handleMessage(Message msg) {  
  43.             int total = msg.getData().getInt("total");  
  44.             progressDialog.setProgress(total);  
  45.             if (total >= 100){  
  46.                 dismissDialog(PROGRESS_DIALOG);  
  47.                 progressThread.setState(ProgressThread.STATE_DONE);  
  48.             }  
  49.         }  
  50.     };   
  51.     /** Nested class that performs progress calculations (counting) */  
  52.     private class ProgressThread extends Thread {  
  53.         Handler mHandler;  
  54.         final static int STATE_DONE = 0;  
  55.         final static int STATE_RUNNING = 1;  
  56.         int mState;  
  57.         int total;  
  58.         ProgressThread(Handler h) {  
  59.             mHandler = h;  
  60.         }  
  61.         public void run() {  
  62.             mState = STATE_RUNNING;     
  63.             total = 0;  
  64.             while (mState == STATE_RUNNING) {  
  65.                 try {  
  66.                     Thread.sleep(100);  
  67.                 } catch (InterruptedException e) {  
  68.                     Log.e("ERROR""Thread Interrupted");  
  69.                 }  
  70.                 Message msg = mHandler.obtainMessage();  
  71.                 Bundle b = new Bundle();  
  72.                 b.putInt("total", total);  
  73.                 msg.setData(b);  
  74.                 mHandler.sendMessage(msg);  
  75.                 total++;  
  76.             }  
  77.         }  
  78.         /* sets the current state for the thread, 
  79.          * used to stop the thread */  
  80.         public void setState(int state) {  
  81.             mState = state;  
  82.         }  
  83.     }  
  84. }  
Creating a Custom Dialog 创建自定义对话框
如果你想自定义一个对话框,你可以使用布局元素来创造你的对话框的布局。定义好布局后,将根View对象或者布局资源ID传给setContentView(View).
例如,创建如图所示的对话框:
创建一个xml布局custom_dialog.xml:
[xhtml]  view plain copy
  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的内容:
[java]  view plain copy
  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)来为对话框设置展开后的布局。例如:
[java]  view plain copy
  1. AlertDialog.Builder builder;  
  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来自定义对话框,可以利用其内置特性例如按钮、选择列表、标题、图标等。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值