zoukankan      html  css  js  c++  java
  • android学习日记27--Dialog使用及其设计模式

    1、Dialog概述
      对话框一般是一个出现在当前Activity之上的一个小窗口,处于下面的Activity失去焦点, 对话框接受所有的用户交互。
    对话框一般用于提示信息和与当前应用程序直接相关的小功能。

    2、Dialog 分类:
      警告对话框 AlertDialog : 一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型。
      进度对话框 ProgressDialog: 显示一个进度环或者一个进度条. 由于它是 AlertDialog 的扩展, 所以它也支持按钮。
      日期选择对话框 ProgressDialog : 让用户选择一个日期。
      时间选择对话框 TimePickerDialog : 让用户选择一个时间。

    3、Dialog应用
      a、AlertDialog一般用法:
        取得创建者的类,AlertDialog.Builder builder = new Builder(Context);然后通过builder.setXX一系列方法来设置属性;
      最后builder.create().show()来显示Dialog。

      b、ProgressDialog、ProgressDialog、TimePickerDialog 用法:

        有些区别,是直接 new XXDialog(Context); 然后通过实例化的dialog.setXX设置属性;最后直接dialog.show()展示。

      c、代码实例

      实现如下9中Dialog

      

      按钮就不一一点进去演示,直接看源码:

      

      1    /**多个按钮信息框 **/
      2     private static final int DIALOG_1 = 2;
      3     /**列表框 **/    
      4     private static final int DIALOG_2 = 3;
      5     /**进度条框 **/
      6     private static final int DIALOG_3 = 4;
      7     /**单项选择列表框 **/
      8     private static final int DIALOG_4 = 5;
      9     /**多项选择列表框 **/
     10     private static final int DIALOG_5 = 6;
     11     /**自定义布局 **/
     12     private static final int DIALOG_6 = 7;
     13     /**读取进度框 **/
     14     private static final int DIALOG_7 = 8;
     15     /**自定义布局 **/
     16     private static final int DIALOG_8 = 9;
     17     /**读取进度框 **/
     18     private static final int DIALOG_9 = 10;
     19     
     20     private ProgressDialog pDialog;
     21     private DatePickerDialog dDialog;
     22     private TimePickerDialog tDialog;
     23     private Calendar c;
     24     final String[] items = {"item0","item1","itme2","item3","itme4"}; 
     25     ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
     26     
     27     @Override
     28     protected void onCreate(Bundle savedInstanceState) {
     29         super.onCreate(savedInstanceState);
     30         setContentView(R.layout.activity_main);
     31         
     32         Button button1 = (Button) findViewById(R.id.button1);
     33         button1.setOnClickListener(new OnClickListener() {
     34             public void onClick(View v) {
     35             CreatDialog(DIALOG_1);
     36             }
     37         });
     38         
     39         Button button2 = (Button) findViewById(R.id.button2);
     40         button2.setOnClickListener(new OnClickListener() {
     41             public void onClick(View v) {
     42             CreatDialog(DIALOG_2);
     43             }
     44         });
     45     
     46         Button button3 = (Button) findViewById(R.id.button3);
     47         button3.setOnClickListener(new OnClickListener() {
     48             public void onClick(View v) {
     49             CreatDialog(DIALOG_3);
     50             //mProgressDialog.setProgress(0);
     51             }
     52         });
     53      
     54         Button button4 = (Button) findViewById(R.id.button4);
     55         button4.setOnClickListener(new OnClickListener() {
     56             public void onClick(View v) {
     57             CreatDialog(DIALOG_4);
     58             }
     59         });  
     60         
     61         Button button5 = (Button) findViewById(R.id.button5);
     62         button5.setOnClickListener(new OnClickListener() {
     63             public void onClick(View v) {
     64             CreatDialog(DIALOG_5);
     65             }
     66         }); 
     67     
     68         Button button6 = (Button) findViewById(R.id.button6);
     69         button6.setOnClickListener(new OnClickListener() {
     70             public void onClick(View v) {
     71             CreatDialog(DIALOG_6);
     72             }
     73         }); 
     74         
     75         Button button7 = (Button) findViewById(R.id.button7);
     76         button7.setOnClickListener(new OnClickListener() {
     77             public void onClick(View v) {
     78             CreatDialog(DIALOG_7);
     79             }
     80         }); 
     81         Button button8 = (Button) findViewById(R.id.button8);
     82         button8.setOnClickListener(new OnClickListener() {
     83             public void onClick(View v) {
     84             CreatDialog(DIALOG_8);
     85             }
     86         }); 
     87         
     88         Button button9 = (Button) findViewById(R.id.button9);
     89         button9.setOnClickListener(new OnClickListener() {
     90             public void onClick(View v) {
     91             CreatDialog(DIALOG_9);
     92             }
     93         });
     94     }
     95     
     96     public void CreatDialog(int id) {
     97         AlertDialog.Builder builder = new Builder(DialogDemoActivity.this);
     98         switch (id) {
     99         case DIALOG_1:
    100             builder.setIcon(R.drawable.ic_launcher);
    101             builder.setTitle("投票");
    102             builder.setMessage("您认为什么样的内容吸引你?");
    103             builder.setPositiveButton("有趣的", new DialogInterface.OnClickListener() {
    104 
    105                 @Override
    106                 public void onClick(DialogInterface dialog, int which) {
    107                     // TODO Auto-generated method stub
    108                     showDialog("您选择了有趣的!");
    109                 }
    110 
    111             });
    112             builder.setNeutralButton("有内涵的", new DialogInterface.OnClickListener() {
    113 
    114                 @Override
    115                 public void onClick(DialogInterface dialog, int which) {
    116                     // TODO Auto-generated method stub
    117                     showDialog("您选择了有内涵的!");
    118                 }
    119 
    120             });
    121             builder.setNegativeButton("其他", new DialogInterface.OnClickListener() {
    122 
    123                 @Override
    124                 public void onClick(DialogInterface dialog, int which) {
    125                     // TODO Auto-generated method stub
    126                     showDialog("您选择了其他!");
    127                 }
    128 
    129             });
    130             break;
    131         case DIALOG_2:
    132             builder.setTitle("列表框");
    133             
    134             builder.setItems(items, new DialogInterface.OnClickListener() {
    135 
    136                 @Override
    137                 public void onClick(DialogInterface dialog, int which) {
    138                     // TODO Auto-generated method stub
    139                     showDialog("您选择了"+items[which]);
    140                 }
    141 
    142             });
    143             break;    
    144         case DIALOG_3:
    145             pDialog = new ProgressDialog(DialogDemoActivity.this);
    146             pDialog.setIcon(R.drawable.ic_launcher);
    147             pDialog.setTitle("带进度条的");
    148             pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    149             pDialog.setMax(100);
    150             pDialog.setButton("ok", new DialogInterface.OnClickListener() {
    151                 
    152                 @Override
    153                 public void onClick(DialogInterface dialog, int which) {
    154                     // TODO Auto-generated method stub
    155                     
    156                 }
    157             });
    158             pDialog.setButton2("cancle", new DialogInterface.OnClickListener() {
    159                 
    160                 @Override
    161                 public void onClick(DialogInterface dialog, int which) {
    162                     // TODO Auto-generated method stub
    163                     
    164                 }
    165             });
    166             pDialog.show();
    167             new Thread(this).start();
    168             return;
    169         case DIALOG_4:
    170             builder.setTitle("单列表选择框");
    171             
    172             builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
    173                 
    174                 @Override
    175                 public void onClick(DialogInterface dialog, int which) {
    176                     // TODO Auto-generated method stub
    177                     showDialog("你选择的id为" + which + " , " + items[which]);
    178                 }
    179             });
    180             break;
    181         case DIALOG_5:
    182             MultiChoiceID.clear();
    183             builder.setTitle("多列表选择框");
    184             
    185             builder.setMultiChoiceItems(items
    186                     , new boolean[]{false,false,false,false,false}
    187             ,new DialogInterface.OnMultiChoiceClickListener() {
    188                 public void onClick(DialogInterface dialog, int whichButton,
    189                         boolean isChecked) {
    190                    if(isChecked) {
    191                        MultiChoiceID.add(whichButton);
    192                        showDialog("你选择的id为" + whichButton + " , " + items[whichButton]);
    193                    }else {
    194                        MultiChoiceID.remove(whichButton);
    195                    }
    196                     
    197                 }
    198             });
    199             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    200                 public void onClick(DialogInterface dialog, int whichButton) {
    201                     String str = "";
    202                     int size = MultiChoiceID.size();
    203                     for (int i = 0 ;i < size; i++) {
    204                     str+= items[MultiChoiceID.get(i)] + ", ";
    205                     }
    206                     showDialog("你选择的是" + str);
    207                 }
    208             });
    209             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    210                 public void onClick(DialogInterface dialog, int whichButton) {
    211 
    212                 }
    213             });
    214             break;
    215         case DIALOG_6:
    216 
    217             builder.setTitle("自定义对话框");
    218             LayoutInflater layoutInflater = LayoutInflater.from(this);
    219             final View layout = layoutInflater.inflate(R.layout.test, null);
    220             
    221             builder.setView(layout);
    222             
    223             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    224                 public void onClick(DialogInterface dialog, int whichButton) {
    225                     EditText userName = (EditText) layout.findViewById(R.id.etUserName);
    226                     EditText password = (EditText) layout.findViewById(R.id.etPassWord);
    227                     showDialog("姓名 :"  + userName.getText().toString()  + "密码:" + password.getText().toString() );
    228                 }
    229             });
    230             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    231                 public void onClick(DialogInterface dialog, int whichButton) {
    232 
    233                 }
    234             });
    235             break;
    236         case DIALOG_7:
    237             pDialog = new ProgressDialog(DialogDemoActivity.this);
    238             pDialog.setIcon(R.drawable.ic_launcher);
    239             pDialog.setTitle("循环进度");
    240             pDialog.setMessage("正在读取");
    241             pDialog.setIndeterminate(true); // 设置进度条不明确,即一直在滚动,不清楚进度
    242             pDialog.setCancelable(true); // 设置 返回键 是否取消 进度框
    243             pDialog.show();
    244             return;
    245         case DIALOG_8:
    246             c= Calendar.getInstance();
    247             dDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    248                 
    249                 @Override
    250                 public void onDateSet(DatePicker view, int year, int monthOfYear,
    251                         int dayOfMonth) {
    252                     // TODO Auto-generated method stub
    253                     
    254                 }
    255             }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
    256             dDialog.show();
    257             return;
    258         case DIALOG_9:
    259             c= Calendar.getInstance();
    260             tDialog = new TimePickerDialog(                //创建TimePickerDialog对象
    261                     this,
    262                     new TimePickerDialog.OnTimeSetListener(){ //创建OnTimeSetListener监听器
    263                         @Override
    264                         public void onTimeSet(TimePicker tp, int hourOfDay, int minute) {
    265                         }                     
    266                      },
    267                      c.get(Calendar.HOUR_OF_DAY),        //传入当前小时数
    268                      c.get(Calendar.MINUTE),            //传入当前分钟数
    269                      false
    270                   );
    271             tDialog.show();
    272             return;
    273         }
    274         
    275         builder.create().show();
    276         
    277     }
    278 
    279     private void showDialog(String str) {
    280             new AlertDialog.Builder(DialogDemoActivity.this)
    281             .setMessage(str)
    282             .show();
    283     }
    284     
    285     @Override
    286     public void run() {
    287         int Progress = 0;
    288         while(Progress < 100) {
    289         try {
    290             Thread.sleep(100);
    291             Progress++;  
    292             pDialog.incrementProgressBy(1);
    293         } catch (InterruptedException e) {
    294             // TODO Auto-generated catch block
    295             e.printStackTrace();
    296         }
    297         }
    298          
    299     }

      其中自定义Dialog的布局文件test.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3 android:layout_height="wrap_content" 
     4 android:layout_width="wrap_content"
     5 android:orientation="horizontal"
     6 android:id="@+id/dialog">
     7 <LinearLayout
     8 android:layout_height="wrap_content" 
     9 android:layout_width="wrap_content"
    10 android:orientation="horizontal"
    11 android:id="@+id/dialogname">
    12 
    13 <TextView android:layout_height="wrap_content"
    14    android:layout_width="wrap_content"
    15   android:id="@+id/tvUserName" 
    16   android:text="姓名:" />
    17 <EditText android:layout_height="wrap_content"
    18   android:layout_width="wrap_content" 
    19   android:id="@+id/etUserName" 
    20   android:minWidth="200dip"/>
    21 </LinearLayout>  
    22 <LinearLayout
    23 android:layout_height="wrap_content" 
    24 android:layout_width="wrap_content"
    25 android:orientation="horizontal"
    26 android:id="@+id/dialognum"
    27  android:layout_below="@+id/dialogname"
    28 >
    29   <TextView android:layout_height="wrap_content"
    30    android:layout_width="wrap_content"
    31   android:id="@+id/tvPassWord" 
    32   android:text="密码:" />
    33 <EditText android:layout_height="wrap_content"
    34   android:layout_width="wrap_content" 
    35   android:id="@+id/etPassWord" 
    36   android:minWidth="200dip"/>
    37  </LinearLayout>  
    38   </RelativeLayout>

    4、Dialog设计模式

      Dialog设计模式中有使用建造者模式,建造者模式将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
    Android中AlertDialog是一个多面手,可以有着不同的样式和呈现,这样通过Builder就可以有效实现构建和表示的分离。
      AlertDialog.Builder就是具体建造者,另外,它是以匿名类的方式被创建的,而且,Builder类是AlertDialog的内部类,这样,
    耦合性比较低,这正是面向对象中要达到的设计意图之一。 最后调用show函数,它的返回类型正是我们要创建的产品,即AlertDialog。
    所以,Builder(具体建造者)是AlertDialog(产品)的内部匿名类,用来创建并显示一个dialog。

  • 相关阅读:
    【题解】P1999 高维正方体
    【题解】 P1850 [NOIP2016 提高组] 换教室(又是一道debug的DP,debug经验++)
    【题解】P1439 【模板】最长公共子序列
    【笔记】还是发上来作为学习过的记录吧,凌乱,勿进
    为什么我不会做数位DP
    【题解】HUD3652 B-number && 数位DP学习笔记
    【题解】LIS(longest increasing subsequence)最长上升子序列
    lingo重点部分快速上手
    koa2转移json文件地址
    Koa2创建项目
  • 原文地址:https://www.cnblogs.com/aiguozhe/p/3690923.html
Copyright © 2011-2022 走看看