Android開發學習之對話框

 對話框式程序運行中彈出的窗口。Android系統中有四種默認的對話框:警告對話框AlertDialog、進度對話框ProgressDialog、日期選擇對話框DatePickerDialog以及時間選擇對話框TimePickerDialog。除此之外,我們自定義自已的dialog。

一. 警告對話框(AlertDialog)

    Android系統中最常用的對話框是AlertDialog,它是一個提示窗口,需要用戶作出選擇的。一般會有幾個按鈕、標題信息、提示信息等。

在程序中創建AlertDialog的步驟:

 1.獲得AlertDialog的靜態內部類Builder對象,由該類來創建對話框,Builder所提供的方法如下: 

setTitle():給對話框設置title.

setIcon():給對話框設置圖標。

setMessage():設置對話框的提示信息

setItems():設置對話框要顯示的一個list,一般用於要顯示幾個命令時

setSingleChoiceItems():設置對話框顯示一個單選的List

setMultiChoiceItems():用來設置對話框顯示一系列的複選框。

setPositiveButton():給對話框添加”Yes”按鈕。

setNegativeButton():給對話框添加”No”按鈕。

2.調用Builder的create( )方法

3.調用AlertDialog的show( )方法顯示對話框

 下面是一個提示信息對話框的實例:

 AlertDialogActivity.java

  1. package com.android.dialog.activity;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. public class AlertDialogActivity extends Activity {
  11. private TextView tv;
  12. private Button btn;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. tv = (TextView)findViewById(R.id.TextView_1);
  18. btn = (Button)findViewById(R.id.Button_1);
  19. //實例化AlertDialog.Builder對象
  20. final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  21. btn.setOnClickListener(new OnClickListener() {
  22. public void onClick(View v) {
  23. //設置提示信息,確定按鈕
  24. builder.setMessage("真的要刪除該文件嗎?").setPositiveButton("是", new DialogInterface.OnClickListener() {
  25. public void onClick(DialogInterface dialog, int which) {
  26. tv.setText("成功刪除");
  27. }
  28. //設置取消按鈕
  29. }).setNegativeButton("否", new DialogInterface.OnClickListener() {
  30. public void onClick(DialogInterface dialog, int which) {
  31. tv.setText("取消刪除");
  32. }
  33. });
  34. //創建對話框
  35. AlertDialog ad = builder.create();
  36. //顯示對話框
  37. ad.show();
  38. }
  39. }
  40. );
  41. }
  42. }

 main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/TextView_1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="測試AlertDialog"
  12. />
  13. <Button
  14. android:id="@+id/Button_1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="刪除文件"
  18. />
  19. </LinearLayout>

 效果圖:

 

 二.進度對話框(ProgressDialog)

在程序中創建ProgressDialog的步驟:

1.覆蓋Activity的onCreateDialog( )方法,並在其中創建對話框

2.調用Activity的showDialog( )方法,顯示進度對話框

下面是一個提示進度對話框的實例:

 ProgressDialogActivity.java

  1. package com.android.progress.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.app.Dialog;  
  5. import android.app.ProgressDialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.  
  12. public class ProgressDialogActivity extends Activity {  
  13.     private Button btn;  
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         btn = (Button)findViewById(R.id.Button_1);  
  19.         btn.setOnClickListener(new OnClickListener() {  
  20.             public void onClick(View v) {  
  21.                 //調用Activity的showDialog()方法,顯示進度對話框  
  22.                 showDialog(0);  
  23.             }  
  24.         });  
  25.     }  
  26.  
  27.     @Override 
  28.     protected Dialog onCreateDialog(int id) {  
  29.         //對進度對話框進行實例化  
  30.         ProgressDialog dialog = new ProgressDialog(this);  
  31.         //設置顯示的標題  
  32.         dialog.setTitle("測試ProgressDialog");  
  33.         dialog.setIndeterminate(true);  
  34.         dialog.setMessage("程序正在Loading...");  
  35.         dialog.setCancelable(true);  
  36.           
  37.         dialog.setButton(Dialog.BUTTON_POSITIVE, "確定",   
  38.                 new DialogInterface.OnClickListener() {  
  39.                     @Override 
  40.                     public void onClick(DialogInterface dialog, int which) {  
  41.                         dialog.cancel();  
  42.                     }  
  43.                 }  
  44.         );  
  45.           
  46.         return dialog;  
  47.     }  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <Button   
  8.         android:id="@+id/Button_1"   
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"   
  11.         android:text="測試ProgressDialog" 
  12.         /> 
  13. </LinearLayout> 

效果圖:

 三.日期,時間選擇對話框(DatePickerDialog、TimePickerDialog)

在程序中創建日期,時間選擇對話框的步驟:

1.覆蓋Activity的onCreateDialog( )方法,並在其中創建對話框

2.分別在OnDateSetListener的onDateSet( )方法和OnTimeSetListener( )的onTimeSet( )事件方法中更改日期,時間

3.調用Activity的showDialog( )方法,顯示進度對話框

 MainActivity.java

  1. package com.android.datatime.activity;  
  2.  
  3. import java.util.Calendar;  
  4. import android.app.Activity;  
  5. import android.app.DatePickerDialog;  
  6. import android.app.Dialog;  
  7. import android.app.TimePickerDialog;  
  8. import android.app.DatePickerDialog.OnDateSetListener;  
  9. import android.app.TimePickerDialog.OnTimeSetListener;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.DatePicker;  
  15. import android.widget.TextView;  
  16. import android.widget.TimePicker;  
  17.  
  18. public class MainActivity extends Activity {  
  19.     private Button btn1,btn2;  
  20.     private TextView tv_1,tv_2;  
  21.     private Calendar c;//獲得日曆實例  
  22.     private int m_year,m_month,m_day;  
  23.     private int m_hour,m_minute;  
  24.       
  25.      
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         btn1 = (Button)findViewById(R.id.Button_1);  
  30.         btn2 = (Button)findViewById(R.id.Button_2);  
  31.           
  32.         c = Calendar.getInstance();  
  33.           
  34.         m_year = c.get(Calendar.YEAR);  
  35.         m_month = c.get(Calendar.MONTH);  
  36.         m_day = c.get(Calendar.DAY_OF_MONTH);  
  37.           
  38.         m_hour = c.get(Calendar.HOUR);  
  39.         m_minute = c.get(Calendar.MINUTE);  
  40.           
  41.         tv_1 = (TextView)findViewById(R.id.TextView_1);  
  42.           
  43.         tv_1.setText(m_year+":"+(m_month+1)+":"+m_day);//設置TextView裏的內容爲日期  
  44.           
  45.         tv_2 = (TextView)findViewById(R.id.TextView_2);  
  46.         tv_2.setText(m_hour+":"+m_minute);//設置TextView裏的內容爲時間  
  47.           
  48.         btn1.setOnClickListener(new OnClickListener() {  
  49.             public void onClick(View v) {  
  50.                 showDialog(0);//顯示日期對話框  
  51.             }  
  52.         });  
  53.           
  54.         btn2.setOnClickListener(new OnClickListener() {  
  55.             public void onClick(View v) {  
  56.                 showDialog(1);//顯示時間對話框  
  57.             }  
  58.         });  
  59.     }  
  60.     //調用Activity的showDialog( )方法顯示進對話框  
  61.     protected Dialog onCreateDialog(int id) {  
  62.         if(id==0)  
  63.            return new DatePickerDialog(this,l1,m_year, m_month, m_day);  
  64.                   
  65.         else 
  66.             return new TimePickerDialog(this, l2, m_hour, m_minute, false);                   
  67.     }  
  68.     //設置日期監聽器  
  69.     private OnDateSetListener l1 = new OnDateSetListener() {  
  70.         public void onDateSet(DatePicker view, int year, int monthOfYear,  
  71.                 int dayOfMonth) {  
  72.             m_year = year;  
  73.             m_month = monthOfYear;  
  74.             m_day = dayOfMonth;  
  75.             tv_1.setText(m_year+":"+(m_month+1)+":"+m_day);//爲TextView設置文本內容,重新顯示日期  
  76.         }  
  77.     };  
  78.     //設置時間監聽器  
  79.     private OnTimeSetListener l2 = new OnTimeSetListener() {  
  80.         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  81.             m_hour = hourOfDay;  
  82.             m_minute = minute;  
  83.             tv_2.setText(m_hour+":"+m_minute);//爲TextView設置文本內容,重新顯示時間  
  84.         }  
  85.     };  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <TextView   
  8.         android:text="" 
  9.         android:id="@+id/TextView_1" 
  10.         android:layout_width="wrap_content" 
  11.         android:layout_height="wrap_content" 
  12.         /> 
  13.     <TextView   
  14.         android:text=""   
  15.         android:id="@+id/TextView_2"   
  16.         android:layout_width="wrap_content"   
  17.         android:layout_height="wrap_content" 
  18.         /> 
  19.     <Button   
  20.         android:text="修改日期"   
  21.         android:id="@+id/Button_1"   
  22.         android:layout_width="wrap_content"   
  23.         android:layout_height="wrap_content" 
  24.         /> 
  25.     <Button   
  26.         android:text="修改時間"   
  27.         android:id="@+id/Button_2"   
  28.         android:layout_width="wrap_content"   
  29.         android:layout_height="wrap_content" 
  30.         /> 
  31. </LinearLayout> 

效果圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章