一、界面效果
運行界面
---------------------------------------------------------------------------------------
部分效果
自定義登錄對話框
圓形(轉圈)進度條
長形進度條
多選按鈕對話框
單選按鈕對話框
帶多個按鈕的提示對話框
帶確定取消按鈕的提示對話框
---------------------------------------------------------------------------------------------------------
二、知識點
1 AlertDialog.Builder屬性
* setTitle:爲對話框設置標題;
* setIcon :爲對話框設置圖標;
* setMessage:爲對話框設置內容;
* setView : 給對話框設置自定義樣式;
* setItems:設置對話框要顯示的一個list,一般用於顯示幾個命令時;
* setMultiChoiceItems:用來設置對話框顯示一系列的複選框;
* setNeutralButton :響應中立行爲的點擊;
* setPositiveButton :響應Yes/Ok的點擊;
* setNegativeButton :響應No/Cancel的點擊;
* create :創建對話框;
* show :顯示對話框;
2 ProgressDialog屬性
*setProgressStyle: 設置進度條風格,風格爲圓形,旋轉的;
*setTitlt: 設置ProgressDialog
標題;
*setMessage: 設置ProgressDialog提示信息;
*setIcon: 設置ProgressDialog標題圖標;
*setIndeterminate: 設置ProgressDialog
的進度條是否不明確;
*setCancelable:
設置ProgressDialog
是否可以按返回鍵取消;
*setButton: 設置ProgressDialog
的一個Button(需要監聽Button事件);
*show:
顯示ProgressDialog。
-----------------------------------------------------------------------------------------
三、源碼
1 佈局文件:dialog_demo.xml
01 |
<STRONG><?xml version="1.0" encoding="utf-8"?> |
|
02 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:layout_width="fill_parent" |
|
04 |
android:layout_height="wrap_content" |
05 |
android:orientation="vertical" > |
|
06 |
|
07 |
<TextView |
|
08 |
android:layout_width="fill_parent" |
09 |
android:layout_height="wrap_content" |
|
10 |
android:gravity="center_horizontal" |
11 |
android:paddingBottom="10dp" |
|
12 |
android:paddingTop="8dp" |
13 |
android:text="SkySeraph Android學習專題:Dialog" |
|
14 |
android:textColor="#FFFF00" |
15 |
android:textSize="15dp" > |
|
16 |
</TextView> |
17 |
|
|
18 |
<LinearLayout |
19 |
android:layout_width="fill_parent" |
|
20 |
android:layout_height="wrap_content" |
21 |
android:layout_gravity="center" |
|
22 |
android:orientation="vertical" > |
23 |
|
|
24 |
<Button |
25 |
android:id="@+id/dialg_demo_btn01" |
|
26 |
android:layout_width="fill_parent" |
27 |
android:layout_height="wrap_content" |
|
28 |
android:text="簡單提示對話框" |
29 |
android:textSize="12dp" /> |
|
30 |
|
31 |
<Button |
|
32 |
android:id="@+id/dialg_demo_btn02" |
33 |
android:layout_width="fill_parent" |
|
34 |
android:layout_height="wrap_content" |
35 |
android:text="帶確定取消按鈕的提示對話框" |
|
36 |
android:textSize="12dp" /> |
37 |
|
|
38 |
<Button |
39 |
android:id="@+id/dialg_demo_btn03" |
|
40 |
android:layout_width="fill_parent" |
41 |
android:layout_height="wrap_content" |
|
42 |
android:text="帶多個按鈕的提示對話框" |
43 |
android:textSize="12dp" /> |
|
44 |
|
45 |
<Button |
|
46 |
android:id="@+id/dialg_demo_btn04" |
47 |
android:layout_width="fill_parent" |
|
48 |
android:layout_height="wrap_content" |
49 |
android:text="單選按鈕對話框" |
|
50 |
android:textSize="12dp" /> |
51 |
|
|
52 |
<Button |
53 |
android:id="@+id/dialg_demo_btn05" |
|
54 |
android:layout_width="fill_parent" |
55 |
android:layout_height="wrap_content" |
|
56 |
android:text="多選按鈕對話框" |
57 |
android:textSize="12dp" /> |
|
58 |
|
59 |
<Button |
|
60 |
android:id="@+id/dialg_demo_btn06" |
61 |
android:layout_width="fill_parent" |
|
62 |
android:layout_height="wrap_content" |
63 |
android:text="列表對話框" |
64 |
android:textSize="12dp" /> |
65 |
|
|
66 |
<Button |
67 |
android:id="@+id/dialg_demo_btn07" |
|
68 |
android:layout_width="fill_parent" |
69 |
android:layout_height="wrap_content" |
|
70 |
android:text="自定義對話框" |
71 |
android:textSize="12dp"/> |
|
72 |
|
73 |
<Button |
|
74 |
android:id="@+id/dialg_demo_btn08" |
75 |
android:layout_width="fill_parent" |
|
76 |
android:layout_height="wrap_content" |
77 |
android:text="長形進度條" |
78 |
android:textSize="12dp"/> |
79 |
|
|
80 |
<Button |
81 |
android:id="@+id/dialg_demo_btn09" |
|
82 |
android:layout_width="fill_parent" |
83 |
android:layout_height="wrap_content" |
|
84 |
android:text="圓形(轉圈)進度條" |
85 |
android:textSize="12dp"/> |
|
86 |
|
87 |
<Button |
|
88 |
android:id="@+id/dialg_demo_btn10" |
89 |
android:layout_width="fill_parent" |
|
90 |
android:layout_height="wrap_content" |
91 |
android:text="..." |
|
92 |
android:textSize="12dp" /> |
93 |
|
|
94 |
</LinearLayout> |
95 |
|
|
96 |
</LinearLayout></STRONG> |
2 java代碼:dialog_demo.java
001 |
public class dialog_demo extends Activity |
|
002 |
{ |
003 |
private static final int MAX_PROGRESS = 100; //進度條最大數 |
004 |
private ProgressDialog mProgressDialog = null; //進度條 |
005 |
final String[] m_Items = {"Frist","Second","Third"}; |
|
006 |
int mSingleChoiceID = -1; //記錄單選中的ID |
007 |
ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();//記錄多選選中的id號 |
|
008 |
|
009 |
// //////////////////////////////////////////////////////////////////////////////////// |
||
010 |
@Override |
011 |
protected void onCreate(Bundle savedInstanceState) |
|
012 |
{ |
013 |
// TODO Auto-generated method stub |
|
014 |
super.onCreate(savedInstanceState); |
015 |
setContentView(R.layout.dialog_demo); |
|
016 |
findViews(); |
017 |
} |
|
018 |
|
019 |
// //////////////////////////////////////////////////////////////////////////////////// |
||
020 |
private void findViews() |
021 |
{ |
||
022 |
// ////////////////////////////////////////////////////////////////////////////// |
023 |
/* 【簡單提示對話框】 */ |
|
024 |
Button btn1 = (Button) findViewById(R.id.dialg_demo_btn01); |
025 |
btn1.setOnClickListener(new OnClickListener() |
|
026 |
{ |
027 |
public void onClick(View v) |
|
028 |
{ |
029 |
// TODO Auto-generated method stub |
030 |
new AlertDialog.Builder(dialog_demo.this).setTitle("簡單提示對話框").setMessage("這是提示信息") |
031 |
.show(); |
|
032 |
return; |
033 |
} |
|
034 |
}); |
035 |
// ////////////////////////////////////////////////////////////////////////////// |
||
036 |
/* 【帶確定取消按鈕的提示對話框】 */ |
037 |
Button btn2 = (Button) findViewById(R.id.dialg_demo_btn02); |
|
038 |
btn2.setOnClickListener(new OnClickListener() |
039 |
{ |
|
040 |
public void onClick(View v) |
041 |
{ |
|
042 |
// TODO Auto-generated method stub |
043 |
AlertDialog.Builder dialog02 = new AlertDialog.Builder(dialog_demo.this); |
044 |
dialog02.setTitle("帶確定取消按鈕的提示對話框"); |
045 |
dialog02.setIcon(R.drawable.qq); |
046 |
dialog02.setMessage("這是提示內容"); |
047 |
dialog02.setPositiveButton("確定", new DialogInterface.OnClickListener() |
|
048 |
{ |
049 |
public void onClick(DialogInterface dialoginterface, int which) |
|
050 |
{ |
051 |
Toast.makeText(dialog_demo.this, "你選擇了確定", Toast.LENGTH_LONG).show(); |
||
052 |
} |
053 |
}); |
|
054 |
dialog02.setNegativeButton("取消", new DialogInterface.OnClickListener() |
055 |
{ |
|
056 |
public void onClick(DialogInterface dialoginterface, int which) |
057 |
{ |
||
058 |
Toast.makeText(dialog_demo.this, "你選擇了取消", Toast.LENGTH_LONG).show(); |
059 |
} |
|
060 |
}); |
061 |
dialog02.create().show(); |
|
062 |
return; |
063 |
} |
|
064 |
}); |
065 |
// ////////////////////////////////////////////////////////////////////////////// |
||
066 |
/* 【帶多個按鈕的提示對話框】 */ |
067 |
Button btn3 = (Button) findViewById(R.id.dialg_demo_btn03); |
|
068 |
btn3.setOnClickListener(new OnClickListener() |
069 |
{ |
|
070 |
public void onClick(View v) |
071 |
{ |
|
072 |
// TODO Auto-generated method stub |
073 |
AlertDialog.Builder dialog03 = new AlertDialog.Builder(dialog_demo.this); |
|
074 |
dialog03.setIcon(R.drawable.img1); |
075 |
dialog03.setTitle("帶多個按鈕的提示對話框"); |
076 |
dialog03.setMessage("你最喜歡的球類運動是什麼呢?"); |
077 |
dialog03.setPositiveButton("籃球", new DialogInterface.OnClickListener() |
|
078 |
{ |
079 |
public void onClick(DialogInterface dialoginterface, int which) |
|
080 |
{ |
081 |
showDialog("籃球很不錯"); |
|
082 |
} |
083 |
}); |
|
084 |
dialog03.setNeutralButton("乒乓球", new DialogInterface.OnClickListener() |
085 |
{ |
|
086 |
public void onClick(DialogInterface dialoginterface, int which) |
087 |
{ |
|
088 |
showDialog("乒乓球很不錯"); |
089 |
} |
|
090 |
}); |
091 |
dialog03.setNegativeButton("足球", new DialogInterface.OnClickListener() |
|
092 |
{ |
093 |
public void onClick(DialogInterface dialoginterface, int which) |
|
094 |
{ |
095 |
showDialog("足球很不錯"); |
|
096 |
} |
097 |
}); |
|
098 |
dialog03.create().show(); |
099 |
return; |
|
100 |
} |
101 |
}); |
||
102 |
// ////////////////////////////////////////////////////////////////////////////// |
103 |
/*【單選按鈕對話框】*/ |
|
104 |
Button btn4 = (Button) findViewById(R.id.dialg_demo_btn04); |
105 |
btn4.setOnClickListener(new OnClickListener() |
|
106 |
{ |
107 |
public void onClick(View v) |
|
108 |
{ |
109 |
// TODO Auto-generated method stub |
|
110 |
mSingleChoiceID = -1; |
111 |
AlertDialog.Builder dialog04 = new AlertDialog.Builder(dialog_demo.this); |
|
112 |
dialog04.setTitle("單選按妞"); |
113 |
dialog04.setSingleChoiceItems(m_Items, 0, new DialogInterface.OnClickListener() |
||
114 |
{ |
115 |
public void onClick(DialogInterface dialog, int whichButton) |
|
116 |
{ |
117 |
mSingleChoiceID = whichButton; |
118 |
showDialog("你選擇的id爲" + whichButton + " , " + m_Items[whichButton]); |
119 |
} |
|
120 |
}); |
121 |
dialog04.setPositiveButton("確定", new DialogInterface.OnClickListener() |
|
122 |
{ |
123 |
public void onClick(DialogInterface dialog, int whichButton) |
|
124 |
{ |
125 |
if (mSingleChoiceID > 0) |
|
126 |
{ |
127 |
showDialog("你選擇的是" + mSingleChoiceID); |
||
128 |
} |
129 |
} |
|
130 |
}); |
131 |
dialog04.setNegativeButton("取消", new DialogInterface.OnClickListener() |
|
132 |
{ |
133 |
public void onClick(DialogInterface dialog, int whichButton) |
|
134 |
{ |
135 |
|
|
136 |
} |
137 |
}); |
|
138 |
dialog04.create().show(); |
139 |
return; |
|
140 |
} |
141 |
}); |
||
142 |
// ////////////////////////////////////////////////////////////////////////////// |
143 |
/*【多選按鈕對話框】*/ |
|
144 |
Button btn5 = (Button) findViewById(R.id.dialg_demo_btn05); |
145 |
btn5.setOnClickListener(new OnClickListener() |
|
146 |
{ |
147 |
public void onClick(View v) |
|
148 |
{ |
149 |
// TODO Auto-generated method stub |
|
150 |
MultiChoiceID.clear(); |
151 |
AlertDialog.Builder dialog05 = new AlertDialog.Builder(dialog_demo.this); |
|
152 |
dialog05.setTitle("多選按鈕"); |
153 |
dialog05.setMultiChoiceItems(m_Items, new boolean[] |
||
154 |
{ false, false, false}, |
155 |
new DialogInterface.OnMultiChoiceClickListener() |
|
156 |
{ |
157 |
public void onClick(DialogInterface dialog, int whichButton, |
|
158 |
boolean isChecked) |
159 |
{ |
||
160 |
if (isChecked) |
161 |
{ |
|
162 |
MultiChoiceID.add(whichButton); |
163 |
showDialog("你選擇的id爲" + whichButton + " , " |
|
164 |
+ m_Items[whichButton]); |
165 |
} else |
166 |
{ |
167 |
MultiChoiceID.remove(whichButton); |
|
168 |
} |
169 |
|
|
170 |
} |
171 |
}); |
|
172 |
dialog05.create().show(); |
173 |
return; |
|
174 |
} |
175 |
}); |
||
176 |
// ////////////////////////////////////////////////////////////////////////////// |
177 |
/*【列表框對話框】*/ |
|
178 |
Button btn6 = (Button) findViewById(R.id.dialg_demo_btn06); |
179 |
btn6.setOnClickListener(new OnClickListener() |
|
180 |
{ |
181 |
public void onClick(View v) |
|
182 |
{ |
183 |
// TODO Auto-generated method stub |
184 |
AlertDialog.Builder dialog06 = new AlertDialog.Builder(dialog_demo.this); |
185 |
dialog06.setTitle("列表框"); |
|
186 |
dialog06.setItems(m_Items, new DialogInterface.OnClickListener() |
187 |
{ |
|
188 |
public void onClick(DialogInterface dialog, int which) |
189 |
{ |
|
190 |
// 點擊後彈出窗口選擇了第幾項 |
191 |
showDialog("你選擇的id爲" + which + " , " + m_Items[which]); |
|
192 |
} |
193 |
}); |
|
194 |
dialog06.create().show(); |
195 |
return; |
|
196 |
} |
197 |
}); |
||
198 |
// ////////////////////////////////////////////////////////////////////////////// |
199 |
/*【自定義登錄對話框】*/ |
|
200 |
Button btn7 = (Button) findViewById(R.id.dialg_demo_btn07); |
201 |
btn7.setOnClickListener(new OnClickListener() |
|
202 |
{ |
203 |
public void onClick(View v) |
|
204 |
{ |
205 |
// TODO Auto-generated method stub |
|
206 |
LayoutInflater factory = LayoutInflater.from(dialog_demo.this); |
207 |
final View view = factory.inflate(R.layout.dialog_demo_login, null);// 獲得自定義對話框 |
|
208 |
|
209 |
AlertDialog.Builder dialog07 = new AlertDialog.Builder(dialog_demo.this); |
210 |
dialog07.setIcon(R.drawable.qq); |
211 |
dialog07.setTitle("自定義登錄對話框"); |
|
212 |
dialog07.setView(view); |
213 |
dialog07.setPositiveButton("確定", new DialogInterface.OnClickListener() |
|
214 |
{ |
215 |
public void onClick(DialogInterface dialog, int whichButton) |
|
216 |
{ |
217 |
|
|
218 |
EditText userName = (EditText) view |
219 |
.findViewById(R.id.dialog_demo_loginETUserName); |
|
220 |
EditText password = (EditText) view |
221 |
.findViewById(R.id.dialog_demo_loginETPassWord); |
|
222 |
showDialog("姓名 :" + userName.getText().toString() + "密碼:" |
223 |
+ password.getText().toString()); |
||
224 |
} |
225 |
}); |
|
226 |
dialog07.setNegativeButton("取消", new DialogInterface.OnClickListener() |
227 |
{ |
|
228 |
public void onClick(DialogInterface dialog, int whichButton) |
229 |
{ |
||
230 |
//Toast.makeText(dialog_demo.this, "你選擇了取消", Toast.LENGTH_LONG).show(); |
231 |
showDialog("你選擇了取消"); |
|
232 |
} |
233 |
}); |
|
234 |
dialog07.create().show(); |
235 |
return; |
|
236 |
} |
237 |
}); |
||
238 |
// ////////////////////////////////////////////////////////////////////////////// |
239 |
Button btn8 = (Button) findViewById(R.id.dialg_demo_btn08); |
|
240 |
btn8.setOnClickListener(new OnClickListener() |
241 |
{ |
|
242 |
public void onClick(View v) |
243 |
{ |
|
244 |
// TODO Auto-generated method stub |
245 |
mProgressDialog = new ProgressDialog(dialog_demo.this);//創建ProgressDialog對象 |
|
246 |
mProgressDialog.setIcon(R.drawable.qq);// 設置ProgressDialog標題 圖標 |
247 |
mProgressDialog.setTitle("進度條窗口");// 設置ProgressDialog標題 |
|
248 |
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設置進度條風格,風格爲長形 |
249 |
mProgressDialog.setMax(MAX_PROGRESS);// 設置ProgressDialo進度條進度 |
|
250 |
mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() |
251 |
{ |
|
252 |
public void onClick(DialogInterface dialog, int whichButton) |
253 |
{ |
|
254 |
// 這裏添加點擊後的邏輯 |
255 |
} |
|
256 |
}); |
257 |
mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() |
|
258 |
{ |
259 |
public void onClick(DialogInterface dialog, int whichButton) |
|
260 |
{ |
261 |
// 這裏添加點擊後的邏輯 |
|
262 |
} |
263 |
}); |
|
264 |
mProgressDialog.show(); |
265 |
new Thread() |
|
266 |
{ |
267 |
@Override |
|
268 |
public void run() |
269 |
{ |
|
270 |
int Progress = 0; |
271 |
while (Progress < MAX_PROGRESS) |
|
272 |
{ |
273 |
try |
||
274 |
{ |
275 |
mProgressDialog.setProgress(Progress++); |
|
276 |
//mProgressDialog.incrementProgressBy(1); |
277 |
Thread.sleep(100); |
|
278 |
} catch (Exception e) |
279 |
{ |
||
280 |
// TODO Auto-generated catch block |
281 |
mProgressDialog.cancel(); |
|
282 |
//e.printStackTrace(); |
283 |
} |
284 |
} |
285 |
}; |
286 |
}.start(); |
287 |
return; |
|
288 |
} |
289 |
}); |
||
290 |
// ////////////////////////////////////////////////////////////////////////////// |
291 |
/*【圓形(轉圈)進度條】*/ |
|
292 |
Button btn9 = (Button) findViewById(R.id.dialg_demo_btn09); |
293 |
btn9.setOnClickListener(new OnClickListener() |
|
294 |
{ |
295 |
public void onClick(View v) |
|
296 |
{ |
297 |
// TODO Auto-generated method stub |
298 |
mProgressDialog = new ProgressDialog(dialog_demo.this);//創建ProgressDialog對象 |
299 |
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //設置進度條風格,風格爲圓形,旋轉的 |
|
300 |
mProgressDialog.setTitle("讀取ing...");// 設置ProgressDialog標題 |
301 |
mProgressDialog.setMessage("正在讀取中請稍候...");// 設置ProgressDialog提示信息 |
|
302 |
mProgressDialog.setIndeterminate(true);//設置ProgressDialog 的進度條不明確 |
303 |
mProgressDialog.setCancelable(true);// 設置ProgressDialog 可以按退回鍵取消 |
|
304 |
mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() |
305 |
{ |
|
306 |
public void onClick(DialogInterface dialog, int whichButton) |
307 |
{ |
|
308 |
// 這裏添加點擊後的邏輯 |
309 |
} |
|
310 |
}); |
311 |
mProgressDialog.show();// 讓ProgressDialog顯示 |
|
312 |
return; |
313 |
} |
|
314 |
}); |
315 |
// ////////////////////////////////////////////////////////////////////////////// |
||
316 |
/*【帶補充對話框】*/ |
317 |
Button btn10 = (Button) findViewById(R.id.dialg_demo_btn10); |
|
318 |
btn10.setOnClickListener(new OnClickListener() |
319 |
{ |
|
320 |
public void onClick(View v) |
321 |
{ |
|
322 |
// TODO Auto-generated method stub |
323 |
return; |
|
324 |
} |
325 |
}); |
||
326 |
// ////////////////////////////////////////////////////////////////////////////// |
327 |
} |
|
328 |
|
329 |
// //////////////////////////////////////////////////////////////////////////////////// |
||
330 |
/*顯示子函數*/ |
331 |
private void showDialog(String str) |
|
332 |
{ |
333 |
new AlertDialog.Builder(dialog_demo.this).setMessage(str).show(); |
334 |
// Toast.makeText(dialog_demo.this, str, Toast.LENGTH_LONG).show(); |
335 |
} |
||
336 |
// //////////////////////////////////////////////////////////////////////////////////// |
337 |
} |
3 自定義登錄對話框:dialog_demo_login.xml
01 |
<?xml version="1.0" encoding="utf-8"?> |
|
02 |
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:id="@+id/dialog_demo_login" |
|
04 |
android:layout_width="fill_parent" |
05 |
android:layout_height="fill_parent" |
|
06 |
android:orientation="vertical" > |
07 |
|
|
08 |
<TextView |
09 |
android:id="@+id/dialog_demo_loginTVUserName" |
|
10 |
android:layout_width="wrap_content" |
11 |
android:layout_height="wrap_content" |
|
12 |
android:text="姓名:" |
13 |
android:textSize="18dp"> |
|
14 |
</TextView> |
15 |
|
|
16 |
<EditText |
17 |
android:id="@+id/dialog_demo_loginETUserName" |
|
18 |
android:layout_width="wrap_content" |
19 |
android:layout_height="wrap_content" |
|
20 |
android:text="" |
21 |
android:textSize="18dp" > |
|
22 |
</EditText> |
23 |
|
|
24 |
<TextView |
25 |
android:id="@+id/dialog_demo_loginTVPassWord" |
|
26 |
android:layout_width="wrap_content" |
27 |
android:layout_height="wrap_content" |
|
28 |
android:text="密碼:" |
29 |
android:textSize="18dp" > |
|
30 |
</TextView> |
31 |
|
|
32 |
<EditText |
33 |
android:id="@+id/dialog_demo_loginETPassWord" |
|
34 |
android:layout_width="wrap_content" |
35 |
android:layout_height="wrap_content" |
|
36 |
android:text="" |
37 |
android:textSize="18dp"> |
|
38 |
</EditText> |
39 |
|
|
40 |
</TableLayout> |
四、Refs
【Android】對話框 AlertDialog :http://blog.csdn.net/feng88724/article/details/6171450
Android UI學習 - 對話框 (AlertDialog & ProgressDialog) http://android.blog.51cto.com/268543/333769
Android軟件開發之盤點所有Dialog對話框大合集(一):http://blog.csdn.net/xys289187120/article/details/6601613
Android
對話框(Dialog)大全建立你自己的對話框:http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html
轉自:http://www.cnblogs.com/skyseraph/archive/2012/02/22/2362989.html
開發者大會最新議題發佈,八折搶票! 2013年1月微軟MVP申請開始啦! “第一次親密接觸”—有獎徵文活動
分類:【Android基礎】 2011-02-01 11:00 14116人閱讀評論(11)收藏舉報
本講介紹一下Android基本組件:對話框AlertDialog。
API:
java.lang.Object |
|
↳ |
android.app.AlertDialog.Builder |
使用AlertDialog.Builder創建對話框需要了解以下幾個方法:
- setTitle :爲對話框設置標題
- setIcon :爲對話框設置圖標
- setMessage:爲對話框設置內容
- setView : 給對話框設置自定義樣式
- setItems :設置對話框要顯示的一個list,一般用於顯示幾個命令時。
- setMultiChoiceItems :用來設置對話框顯示一系列的複選框。
- setNeutralButton :
- setPositiveButton :給對話框添加"Yes"按鈕
- setNegativeButton :對話框添加"No"按鈕
- create : 創建對話框
- show :顯示對話框
下面我們來看一下最簡單對話框。
這個對話框只是簡單的顯示內容,使用默認圖標,沒有按鈕,不多說,貼代碼:
[java] view plaincopyprint?
1. new AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示標題").setMessage("這是提示內容").show();
(Lesson_02_Dia是類名,請換成自己的!!)
下面我們爲這個對話框加個按鈕,效果:
代碼:
[java] view plaincopyprint?
1. new AlertDialog.Builder(Lesson_01_Pic.this)
2. .setTitle("這是標題")
3. .setMessage("這是提示內容")
4. .setPositiveButton("確定",
5. new DialogInterface.OnClickListener(){
6. public void onClick(DialogInterface dialoginterface, int i){
7. //按鈕事件
8. Toast.makeText(Lesson_01_Pic.this, "確定",Toast.LENGTH_LONG).show();
9. }
10. }).show();
添加按鈕時,需要同時爲該按鈕指定監聽器。
下面,我們修改一個圖標,添加兩個按鈕,同時顯示多個選項,先看下效果:
代碼:
[c-sharp] view plaincopyprint?
1. package com.yfz;
2. import android.app.Activity;
3. import android.app.AlertDialog;
4. import android.app.Dialog;
5. import android.content.DialogInterface;
6. import android.content.DialogInterface.OnClickListener;
7. import android.content.DialogInterface.OnMultiChoiceClickListener;
8. import android.os.Bundle;
9. import android.view.View;
10. import android.widget.Button;
11. import android.widget.Toast;
12. public class Lesson_02_Dia extends Activity {
13. /** Called when the activity is first created. */
14. @Override
15. public void onCreate(Bundle savedInstanceState) {
16. super.onCreate(savedInstanceState);
17. setContentView(R.layout.main);
18.
19. Button button = (Button)findViewById(R.id.b01);
20. button.setText("對話框");
21. button.setOnClickListener(new Button.OnClickListener(){
22. @Override
23. public void onClick(View v) {
24. //選項數組
25. String[] choices={"Facebook","Twitter"};
26. //Check判斷數組,與選項對應
27. boolean[] chsBool = {true,false};
28. //包含多個選項及複選框的對話框
29. AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)
30. .setIcon(android.R.drawable.btn_star_big_on)
31. .setTitle("調查")
32. .setMultiChoiceItems(choices, chsBool, multiClick)
33. .setPositiveButton("Yes", onclick)
34. .setNegativeButton("No", onclick).create();
35. dialog.show();
36. }
37.
38. });
39. }
40.
41. /**
42. * 對話框複選框事件監聽器
43. */
44. OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){
45. @Override
46. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
47. Toast.makeText(Lesson_02_Dia.this, "第"+(which+1)+"項,選中結果:"+isChecked,Toast.LENGTH_SHORT).show();
48. }
49.
50. };
51.
52. /**
53. * 對話框按鈕點擊事件監聽器
54. */
55. OnClickListener onclick = new OnClickListener() {
56. @Override
57. public void onClick(DialogInterface dialog, int which) {
58. switch (which) {
59. case Dialog.BUTTON_NEGATIVE:
60. Toast.makeText(Lesson_02_Dia.this, "No..",
61. Toast.LENGTH_LONG).show();
62. break;
63. case Dialog.BUTTON_NEUTRAL:
64. Toast.makeText(Lesson_02_Dia.this, "I don't know.",
65. Toast.LENGTH_LONG).show();
66. break;
67. case Dialog.BUTTON_POSITIVE:
68. Toast.makeText(Lesson_02_Dia.this, "Yes!!",
69. Toast.LENGTH_LONG).show();
70. break;
71. }
72. }
73. };
74. }
說明已經寫在註釋中了。
下面再介紹一種比較常用的式樣,如圖:
代碼:
[java] view plaincopyprint?
1. @Override
2. public void onCreate(Bundle savedInstanceState) {
3. super.onCreate(savedInstanceState);
4. setContentView(R.layout.main);
5.
6. Button button = (Button)findViewById(R.id.b01);
7. button.setText("對話框");
8. button.setOnClickListener(new Button.OnClickListener(){
9. @Override
10. public void onClick(View v) {
11. //選項數組
12. String[] choices={"新浪微博","校內","街旁"};
13. //包含多個選項的對話框
14. AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)
15. .setIcon(android.R.drawable.btn_star)
16. .setTitle("分享")
17. .setItems(choices, onselect).create();
18. dialog.show();
19. }
20. });
21. }
22.
23.
24. /**
25. * 選項的事件監聽器
26. */
27. OnClickListener onselect = new OnClickListener() {
28. @Override
29. public void onClick(DialogInterface dialog, int which) {
30. // TODO Auto-generated method stub
31. switch (which) {
32. case 0:
33. Toast.makeText(Lesson_02_Dia.this, "您選擇了新浪微博.",Toast.LENGTH_SHORT).show();
34. break;
35. case 1:
36. Toast.makeText(Lesson_02_Dia.this, "您選擇了校內",Toast.LENGTH_SHORT).show();
37. break;
38. case 2:
39. Toast.makeText(Lesson_02_Dia.this, "您選擇了街旁",Toast.LENGTH_SHORT).show();
40. break;
41. }
42. }
43.
44. };
好了,今天就寫到這,改天寫一下,如果在彈出框中做一個登陸界面。
繼續補充...先上圖...
頁面login.xml: 示例寫的比較簡單,佈局大家可以自己完善、修改。
[xhtml] view plaincopyprint?
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout
3. android:id="@+id/widget36"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:orientation="vertical"
7. xmlns:android="http://schemas.android.com/apk/res/android"
8. >
9. <TextView
10. android:id="@+id/widget37"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:text="用戶名:"
14. >
15. </TextView>
16. <EditText
17. android:id="@+id/widget38"
18. android:layout_width="wrap_content"
19. android:layout_height="wrap_content"
20. android:text=""
21. android:textSize="18sp"
22. >
23. </EditText>
24. <TextView
25. android:id="@+id/widget39"
26. android:layout_width="wrap_content"
27. android:layout_height="wrap_content"
28. android:text="密碼:"
29. >
30. </TextView>
31. <EditText
32. android:id="@+id/widget40"
33. android:layout_width="wrap_content"
34. android:layout_height="wrap_content"
35. android:text=""
36. android:textSize="18sp"
37. >
38. </EditText>
39. </TableLayout>
代碼 : (也比較簡單)
[c-sharp] view plaincopyprint?
1. LayoutInflater factory = LayoutInflater.from(Lesson_02_Dia.this);
2. //獲得自定義對話框
3. View view = factory.inflate(R.layout.login, null);
4.
5. AlertDialog dialog02 = new AlertDialog.Builder(Lesson_02_Dia.this)
6. .setIcon(android.R.drawable.btn_star)
7. .setTitle("登錄")
8. .setView(view)
9. .setPositiveButton("Yes", onclick)
10. .setNegativeButton("No", onclick).create();
11. dialog02.show();
·8樓 [遊客] 2012-08-10 16:48發表[回覆][引用][舉報][刪除]