对话框vAndroid提供了四种常用对话框:

 

AlertDialog:功能最丰富,应用最广的对话框
ProgressDialog:进度对话框
DatePickerDialog:日期选择对话框
TimePickerDialog:时间选择对话框
 
 创建步骤:
1.创建Builder
2.设置信息,标题,添加按钮...
.setTitle("");
.setMessage("");
.builder.setPositiveButton("Title",onClickListener());
3.调用builder.create().show();
   或者使用showDialog(int);调用,并且重写onCreateDialog方法,onCreateDialog方法里面建立builder

 

 

 

这句话可以添加风格化格式,浮于所有界面上
<activity  android:theme="@android:style/Theme.Dialog">

下面是普通的dialog:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView   
  8.         android:id="@+id/alert_txt" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:hint="这个是alert测试"/> 
  12.     <Button   
  13.         android:id="@+id/alert_btn" 
  14.         android:layout_width="wrap_content" 
  15.         android:layout_height="wrap_content" 
  16.         android:text="点我试试"/> 
  17. </LinearLayout> 
  1. import android.app.Activity;  
  2. import android.app.AlertDialog;  
  3. import android.app.AlertDialog.Builder;  
  4. import android.content.DialogInterface;  
  5. import android.content.DialogInterface.OnClickListener;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.  
  11. public class Alertdialog extends Activity {  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState)  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.alertdialog);  
  17.         Button bn = (Button)findViewById(R.id.alert_btn);  
  18.         //定义一个AlertDialog.Builder对象  
  19.         final Builder builder = new AlertDialog.Builder(this);  
  20.         //为按钮绑定事件监听器  
  21.         bn.setOnClickListener(new View.OnClickListener()  
  22.         {  
  23.             public void onClick(View source)  
  24.             {  
  25.                 // 设置对话框的图标  
  26.                 builder.setIcon(R.drawable.tools);  
  27.                 // 设置对话框的标题  
  28.                 builder.setTitle("自定义普通对话框");  
  29.                 // 设置对话框显示的内容  
  30.                 builder.setMessage("一个简单的提示对话框");  
  31.                 // 为对话框设置一个“确定”按钮  
  32.                 builder.setPositiveButton("确定" 
  33.                     //为列表项的单击事件设置监听器  
  34.                     , new OnClickListener()  
  35.                     {  
  36.                         public void onClick(DialogInterface dialog, int which)  
  37.                         {  
  38.                             TextView show = (TextView) findViewById(R.id.alert_txt);  
  39.                             // 设置EditText内容  
  40.                             show.setText("用户单击了“确定”按钮!");  
  41.                         }  
  42.                     });  
  43.                 // 为对话框设置一个“取消”按钮  
  44.                 builder.setNegativeButton("取消" ,  new OnClickListener()  
  45.                     {  
  46.                         public void onClick(DialogInterface dialog, int which)  
  47.                         {  
  48.                             TextView show = (TextView) findViewById(R.id.alert_txt);  
  49.                             // 设置EditText内容  
  50.                             show.setText("用户单击了“取消”按钮!");  
  51.                         }  
  52.                     });  
  53.                 //创建、并显示对话框  
  54.                 builder.create().show();  
  55.             }  
  56.         });  
  57.     }  
  58. }  

 

 

 

AlertDialog对话框_职场

DatePickerDialog:与TimePickerDialog用法一样

 

  1. import java.util.Calendar;  
  2. import android.app.Activity;  
  3. import android.app.DatePickerDialog;  
  4. import android.app.Dialog;  
  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.DatePicker;  
  10. import android.widget.TextView;  
  11.  
  12. public class Alertdialog_datepicker extends Activity{  
  13.     Calendar calendar=Calendar.getInstance();  
  14.     TextView textView;  
  15.     @Override 
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.alertdialog_datepick);  
  19.           
  20.         textView=(TextView) findViewById(R.id.datepicker_txt);  
  21.         textView.setText("当前时间为:"+calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日");  
  22.           
  23.         Button btn=(Button) findViewById(R.id.datepicker_btn);  
  24.           
  25. //******************************设置时间对话框******************************************//  
  26.         btn.setOnClickListener(new OnClickListener() {  
  27.             @Override 
  28.             public void onClick(View v) {  
  29.                 showDialog(111);  
  30. //              new DatePickerDialog(Alertdialog_datepicker.this,   
  31. //                  new DatePickerDialog.OnDateSetListener() {  
  32. //                      @Override  
  33. //                      public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {  
  34. //                          textView.setText("当前时间为:"+year+"年"+monthOfYear+"月"+dayOfMonth+"日");  
  35. //                      }  
  36. //              }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();  
  37.             }  
  38.         });  
  39.  
  40.     }  
  41.     @Override 
  42.     protected Dialog onCreateDialog(int id, Bundle args) {  
  43.         return new DatePickerDialog(Alertdialog_datepicker.this,   
  44.                 new DatePickerDialog.OnDateSetListener() {  
  45.                     @Override 
  46.                     public void onDateSet(DatePicker view, int year, int monthOfYear,  
  47.                             int dayOfMonth) {  
  48.                         textView.setText("当前时间为:"+year+"年"+monthOfYear+"月"+dayOfMonth+"日");  
  49.                     }  
  50.         }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));  
  51.     }  
  52.       
  53.       

 

ProgressDialog:进度条对话框

 

  1. import java.util.Calendar;  
  2. import android.app.Activity;  
  3. import android.app.Dialog;  
  4. import android.app.ProgressDialog;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class Alertdialog_progress extends Activity{  
  11.     Calendar calendar=Calendar.getInstance();  
  12.     ProgressDialog pd;  
  13.     int status=0;  
  14.     @Override 
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.alertdialog_progress);  
  18.           
  19.         Button btn=(Button) findViewById(R.id.progress_btn);  
  20.           
  21. //************************************************************************************//  
  22.         btn.setOnClickListener(new OnClickListener() {  
  23.             @Override 
  24.             public void onClick(View v) {  
  25.                 showDialog(111);  
  26.                 //新建一个线程,执行进度递增  
  27.                 new Thread(){  
  28.                     @Override 
  29.                     public void run() {  
  30.                         doprogress();  
  31.                     }  
  32.                 }.start();  
  33.             }  
  34.         });  
  35.  
  36.     }  
  37.     @Override 
  38.     protected Dialog onCreateDialog(int id, Bundle args) {  
  39.         pd=new ProgressDialog(this);  
  40.         pd.setMax(100);  
  41.         // 设置对话框的标题  
  42.         pd.setTitle("任务完成百分比");  
  43.         // 设置对话框 显示的内容  
  44.         pd.setMessage("耗时任务的完成百分比");  
  45.         // 设置对话框不能用“取消”按钮关闭  
  46.         pd.setCancelable(false);  
  47.         // 设置对话框的进度条风格  
  48. //      pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  49.         pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  50.         // 设置对话框的进度条是否显示进度  
  51.         pd.setIndeterminate(false);  
  52.  
  53.         return pd;  
  54.     }  
  55.     //增加进度条进度  
  56.     private void doprogress(){  
  57.         //当进度小于100的时候,每100毫秒增加1  
  58.         while (pd.getProgress()<100) {  
  59.             try {  
  60.                 Thread.sleep(100);  
  61.             } catch (InterruptedException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.             status++;  
  65.             pd.setProgress(status);  
  66.         }  
  67.         //进度要100的时候隐藏进度条,并且进度归零  
  68.         if (pd.getProgress()>=100) {  
  69.             pd.dismiss();  
  70.             status=0;  
  71.             pd.setProgress(status);  
  72.         }  
  73.           
  74.     }  
  75.  
  76.       

 

下面还有一些列表,单选,多选对话框:

 

  1. import android.app.Activity;  
  2. import android.app.AlertDialog;  
  3. import android.app.AlertDialog.Builder;  
  4. import android.content.DialogInterface;  
  5. import android.content.DialogInterface.OnClickListener;  
  6. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.  
  12. public class Alertdialog_item extends Activity{  
  13.  
  14.     @Override 
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.alertdialog_item);  
  18.           
  19.         final TextView textView=(TextView) findViewById(R.id.alert_item_txt);  
  20.         Button btn=(Button) findViewById(R.id.alert_item_btn);  
  21.         Button btn2=(Button) findViewById(R.id.alert_item_btn2);  
  22.         Button btn3=(Button) findViewById(R.id.alert_item_btn3);  
  23.           
  24. //******************************列表对话框******************************************//  
  25.         btn.setOnClickListener(new View.OnClickListener() {  
  26.             @Override 
  27.             public void onClick(View v) {  
  28.                 final String[] str=new String[]{"第一个选择框","第二个选择框","第三个选择框"};  
  29.                 //创建选择对话框  
  30.                 Builder builder=new AlertDialog.Builder(Alertdialog_item.this);  
  31.                 builder.setIcon(R.drawable.tools);  
  32.                 builder.setTitle("列表对话框");  
  33.                 builder.setPositiveButton("ok"null);  
  34.                 builder.setItems(str,new OnClickListener() {  
  35.                     @Override 
  36.                     public void onClick(DialogInterface dialog, int which) {  
  37.                         textView.setText(str[which]);  
  38.                     }  
  39.                 });  
  40.                 builder.create().show();  
  41.             }  
  42.         });  
  43.  
  44. //******************************单选对话框******************************************//  
  45.         btn2.setOnClickListener(new View.OnClickListener() {  
  46.             @Override 
  47.             public void onClick(View v) {  
  48.                 final String[] str=new String[]{"第四个选择框","第五个选择框","第六个选择框"};  
  49.                 final boolean[] res=new boolean[]{false,false,false};  
  50.                 //创建对话框  
  51.                 Builder builder=new AlertDialog.Builder(Alertdialog_item.this);  
  52.                 builder.setIcon(R.drawable.tools);  
  53.                 builder.setTitle("单选对话框");  
  54.                 builder.setSingleChoiceItems(str,0new OnClickListener() {  
  55.                     @Override 
  56.                     public void onClick(DialogInterface dialog, int which) {  
  57.                         textView.setText(str[which]);  
  58.                     }  
  59.                 });  
  60.                 builder.setPositiveButton("ok"null);  
  61.                 builder.create().show();  
  62.             }  
  63.         });  
  64.  
  65. //******************************多选对话框******************************************//  
  66.         btn3.setOnClickListener(new View.OnClickListener() {  
  67.             @Override 
  68.             public void onClick(View v) {  
  69.                 final String[] str=new String[]{"第七个选择框","第八个选择框","第九个选择框"};  
  70.                 final boolean[] res=new boolean[]{false,false,false};  
  71.                 //创建对话框  
  72.                 Builder builder=new AlertDialog.Builder(Alertdialog_item.this);  
  73.                 builder.setIcon(R.drawable.tools);  
  74.                 builder.setTitle("多选对话框");  
  75.                 builder.setMultiChoiceItems(str,   
  76.                         res,new OnMultiChoiceClickListener() {  
  77.                             @Override 
  78.                             public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  79.                                 String str2="";  
  80.                                 for (int i = 0; i < str.length; i++) {  
  81.                                     if (res[i]) {  
  82.                                         str2+=str[i]+" ";  
  83.                                     }     
  84.                                 }  
  85.                                   
  86.                                 textView.setText(str2);  
  87.                                   
  88.                             }  
  89.                         });  
  90.                 builder.setPositiveButton("ok"null);  
  91.                 builder.create().show();  
  92.             }  
  93.         });  
  94.  
  95.           
  96.     }  
  97.       
  98. }  

还有自定义对话框:定义了一个列表式的dialog

 

 

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.app.AlertDialog.Builder;  
  8. import android.content.DialogInterface;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.SimpleAdapter;  
  14.  
  15. public class Alertdialog_defined extends Activity{  
  16.  
  17.     @Override 
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.alertdialog_defined);  
  21.           
  22.         Button btn=(Button) findViewById(R.id.defined_btn);  
  23.           
  24. //******************************自定义话框******************************************//  
  25.         btn.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override 
  28.             public void onClick(View v) {  
  29.                 int[] ic=new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4};  
  30.                 List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  31.                   
  32.                 for (int i = 0; i <4; i++) {  
  33.                     Map<String, Object> map=new HashMap<String, Object>();  
  34.                     map.put("p_w_picpath", ic[i]);  
  35.                     map.put("txt", ic[i]);  
  36.                     list.add(map);  
  37.                 }  
  38.                 //创建SimpleAdapter  
  39.                 SimpleAdapter simpleAdapter=new SimpleAdapter(Alertdialog_defined.this, list,R.layout.alertdialog_defined2 ,new String[]{"p_w_picpath","txt"},new int[]{R.id.defined_p_w_picpath,R.id.defined_txt});  
  40.                 Builder builder=new AlertDialog.Builder(Alertdialog_defined.this);  
  41.                 //设置标题头  
  42.                 builder.setTitle("自定义对话框");  
  43.                 //添加带图片列表,不做任何动作  
  44.                 builder.setAdapter(simpleAdapter,new DialogInterface.OnClickListener() {  
  45.                     @Override 
  46.                     public void onClick(DialogInterface dialog, int which) {  
  47.                               
  48.                     }  
  49.                 });  
  50.                 //添加按钮,点击对话框消失,没有任何事件  
  51.                 builder.setPositiveButton("ok"null);  
  52.                 builder.create().show();  
  53.             }  
  54.         });  
  55. //******************************************************************************************//  
  56. //或者重写onCreateDialog方法,return一个Dialog类型builder.create()  
  57. //用showDialog(int);调用  
  58.         /*btn.setOnClickListener(new OnClickListener() {  
  59.             @Override  
  60.             public void onClick(View v) {  
  61.                 showDialog(111);  
  62.             }  
  63.         });  
  64.  
  65.     }  
  66.     @Override  
  67.     protected Dialog onCreateDialog(int id, Bundle args) {  
  68.         int[] ic=new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4};  
  69.         List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  70.           
  71.         for (int i = 0; i <4; i++) {  
  72.             Map<String, Object> map=new HashMap<String, Object>();  
  73.             map.put("p_w_picpath", ic[i]);  
  74.             map.put("txt", ic[i]);  
  75.             list.add(map);  
  76.         }  
  77.         SimpleAdapter simpleAdapter=new SimpleAdapter(Alertdialog_defined.this, list,R.layout.alertdialog_defined2 ,new String[]{"p_w_picpath","txt"},new int[]{R.id.defined_p_w_picpath,R.id.defined_txt});  
  78.         Builder builder=new AlertDialog.Builder(Alertdialog_defined.this);  
  79.         builder.setTitle("自定义对话框");  
  80.         builder.setAdapter(simpleAdapter,new DialogInterface.OnClickListener() {  
  81.             @Override  
  82.             public void onClick(DialogInterface dialog, int which) {  
  83.                       
  84.             }  
  85.         });  
  86.         builder.setPositiveButton("ok", null);  
  87.         return builder.create();  
  88.     }*/ 
  89. }  
  90. }  

AlertDialog对话框_休闲_02