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="https://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。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *