Android dialog用法(二)

来源:互联网 发布:改号软件app 编辑:程序博客网 时间:2024/05/07 09:36

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.packagecom.example.progressdialog;
viewplain
  1. importandroid.app.Activity;
  2. importandroid.app.Dialog;
  3. importandroid.app.ProgressDialog;
  4. importandroid.os.Bundle;
  5. importandroid.os.Handler;
  6. importandroid.os.Message;
  7. importandroid.view.View;
  8. importandroid.view.View.OnClickListener;
  9. importandroid.widget.Button;
  10. publicclass NotificationTest extends Activity {
  11. staticfinal int PROGRESS_DIALOG = 0;
  12. Button button;
  13. ProgressThreadprogressThread;
  14. ProgressDialogprogressDialog;
  15. publicvoid 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(newOnClickListener(){
  21. publicvoid onClick(View v) {
  22. showDialog(PROGRESS_DIALOG);
  23. }
  24. });
  25. }
  26. protectedDialog onCreateDialog(intid) {
  27. switch(id){
  28. casePROGRESS_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. returnprogressDialog;
  35. default:
  36. returnnull;
  37. }
  38. }
  39. // Define the Handler that receives messages fromthe thread and update the progress
  40. finalHandler handler = new Handler() {
  41. publicvoid handleMessage(Message msg) {
  42. inttotal = 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. privateclass ProgressThread extends Thread {
  51. Handler mHandler;
  52. finalstatic int STATE_DONE = 0;
  53. finalstatic int STATE_RUNNING = 1;
  54. intmState;
  55. inttotal;
  56. ProgressThread(Handler h){
  57. mHandler = h;
  58. }
  59. publicvoid 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. publicvoid setState(intstate) {
  77. mState = state;
  78. }
  79. }
  80. }
Creating a Custom Dialog 创建自定义对话框
如果你想自定义一个对话框,你可以使用布局元素来创造你的对话框的布局。定义好布局后,将根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来自定义对话框,可以利用其内置特性例如按钮、选择列表、标题、图标等。

0 0
原创粉丝点击