1 page.title=Hp thoi 2 page.tags=alertdialog,dialogfragment 3 4 @jd:body 5 6 7 8 <div id="qv-wrapper"> 9 <div id="qv"> 10 <h2>Trong ti liu ny</h2> 11 <ol> 12 <li><a href="#DialogFragment">To mt Phn on Hp thoi</a></li> 13 <li><a href="#AlertDialog">Xy dng mt Hp thoi Cnh bo</a> 14 <ol> 15 <li><a href="#AddingButtons">Thm nt</a></li> 16 <li><a href="#AddingAList">Thm mt danh sch</a></li> 17 <li><a href="#CustomLayout">To mt B tr Ty chnh</a></li> 18 </ol> 19 </li> 20 <li><a href="#PassingEvents">Chuyn S kin li My ch ca Hp thoi</a></li> 21 <li><a href="#ShowingADialog">Hin th mt Hp thoi</a></li> 22 <li><a href="#FullscreenDialog">Hin th mt Hp thoi Ton mn hnh hoc dng mt Phn on Nhng</a> 23 <ol> 24 <li><a href="#ActivityAsDialog">Hin th mt hot ng di dng mt hp thoi trn mn hnh ln</a></li> 25 </ol> 26 </li> 27 <li><a href="#DismissingADialog">B mt Hp thoi</a></li> 28 </ol> 29 30 <h2>Lp kha</h2> 31 <ol> 32 <li>{@link android.app.DialogFragment}</li> 33 <li>{@link android.app.AlertDialog}</li> 34 </ol> 35 36 <h2>Xem thm</h2> 37 <ol> 38 <li><a href="{@docRoot}design/building-blocks/dialogs.html">Hng dn thit k hp thoi</a></li> 39 <li><a href="{@docRoot}guide/topics/ui/controls/pickers.html">B chn</a> (Hp thoi Ngy/Gi)</li> 40 </ol> 41 </div> 42 </div> 43 44 <p>Hp thoi l mt ca s nh c chc nng nhc ngi dng 45 a ra mt quyt nh hoc nhp thng tin b sung. Hp thoi khng lp kn mn hnh v 46 thng c s dng cho cc s kin m thi yu cu ngi dng phi thc hin mt hnh ng trc khi c th i tip.</p> 47 48 <div class="note design"> 49 <p><strong>Thit k Hp thoi</strong></p> 50 <p> bit thng tin v cch thit k hp thoi ca bn, bao gm cc xut 51 v ngn ng, hy c hng dn thit k <a href="{@docRoot}design/building-blocks/dialogs.html">Hp thoi</a>.</p> 52 </div> 53 54 <img src="{@docRoot}images/ui/dialogs.png" /> 55 56 <p>Lp {@link android.app.Dialog} l lp c s cho hp thoi, nhng bn 57 nn trnh khi to {@link android.app.Dialog} mt cch trc tip. 58 Thay vo , hy s dng mt trong cc lp con sau:</p> 59 <dl> 60 <dt>{@link android.app.AlertDialog}</dt> 61 <dd>Hp thoi c th hin th mt tiu , ti a ba nt, mt danh sch 62 cc mc c th chn, hoc mt b tr ty chnh.</dd> 63 <dt>{@link android.app.DatePickerDialog} hoc {@link android.app.TimePickerDialog}</dt> 64 <dd>Hp thoi vi mt UI c xc nh trc, cho php ngi dng chn ngy hoc gi.</dd> 65 </dl> 66 67 <div class="sidebox"> 68 <h2>Trnh ProgressDialog</h2> 69 <p>Android c mt lp hp thoi khc gi l 70 {@link android.app.ProgressDialog}, n hin th mt hp thoi vi mt thanh tin . Tuy nhin, nu bn 71 cn ch bo tin ti hoc cha xc nh, thay vo , bn nn tun theo hng dn 72 thit k dnh cho <a href="{@docRoot}design/building-blocks/progress.html">Tin & 73 Hot ng</a> v s dng mt {@link android.widget.ProgressBar} trong b tr ca mnh.</p> 74 </div> 75 76 <p>Nhng lp ny nh ngha kiu v cu trc cho hp thoi ca bn, nhng bn nn 77 s dng mt {@link android.support.v4.app.DialogFragment} lm b cha cho hp thoi ca mnh. 78 Lp {@link android.support.v4.app.DialogFragment} s cung cp tt c iu khin m 79 bn cn to hp thoi ca mnh v qun l din mo ca hp thoi, thay v gi ra cc phng php 80 trn i tng {@link android.app.Dialog}.</p> 81 82 <p>Vic s dng {@link android.support.v4.app.DialogFragment} qun l hp thoi 83 s m bo rng n x l ng cc s kin vng i 84 chng hn nh khi ngi dng nhn nt <em>Quay li</em> hoc xoay mn hnh. Lp {@link 85 android.support.v4.app.DialogFragment} cng cho php bn s dng li UI ca hp thoi nh mt 86 thnh phn c th nhng trong mt UI rng hn, ging nh mt {@link 87 android.support.v4.app.Fragment} truyn thng (chng hn nh khi bn mun UI hp thoi xut hin khc i 88 trn cc mn hnh ln v nh).</p> 89 90 <p>Cc phn sau trong hng dn ny m t cch s dng {@link 91 android.support.v4.app.DialogFragment} kt hp vi mt i tng {@link android.app.AlertDialog} 92 . Nu mun to mt b chn ngy hoc gi, thay vo , bn nn c hng dn 93 <a href="{@docRoot}guide/topics/ui/controls/pickers.html">B chn</a>.</p> 94 95 <p class="note"><strong>Lu :</strong> 96 V lp {@link android.app.DialogFragment} ban u c b sung cng vi 97 Android 3.0 (API mc 11), ti liu ny m t cch s dng lp {@link 98 android.support.v4.app.DialogFragment} c cung cp km <a href="{@docRoot}tools/support-library/index.html">Th vin H tr</a>. Bng cch thm th vin ny 99 vo ng dng ca mnh, bn c th s dng {@link android.support.v4.app.DialogFragment} v nhiu loi 100 API khc trn cc thit b chy Android 1.6 hoc cao hn. Nu phin bn ti thiu m ng dng ca bn h tr 101 l API mc 11 hoc cao hn, khi bn c th s dng phin bn khun kh ca {@link 102 android.app.DialogFragment}, nhng hy ch rng cc lin kt trong ti liu ny dnh cho cc API 103 th vin h tr. Khi s dng th vin h tr, 104 hy nh rng bn nhp lp <code>android.support.v4.app.DialogFragment</code> 105 ch <em>khng phi</em> <code>android.app.DialogFragment</code>.</p> 106 107 108 <h2 id="DialogFragment">To mt Phn on Hp thoi</h2> 109 110 <p>Bn c th hon thnh nhiu loi thit k hp thoi—bao gm 111 b tr ty chnh v nhng b tr c m t trong hng dn thit k <a href="{@docRoot}design/building-blocks/dialogs.html">Hp thoi</a> 112 —bng cch m rng 113 {@link android.support.v4.app.DialogFragment} v to mt {@link android.app.AlertDialog} 114 trong phng php gi li {@link android.support.v4.app.DialogFragment#onCreateDialog 115 onCreateDialog()}.</p> 116 117 <p>V d, sau y l mt {@link android.app.AlertDialog} c bn c qun l bn trong 118 mt {@link android.support.v4.app.DialogFragment}:</p> 119 120 <pre> 121 public class FireMissilesDialogFragment extends DialogFragment { 122 @Override 123 public Dialog onCreateDialog(Bundle savedInstanceState) { 124 // Use the Builder class for convenient dialog construction 125 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 126 builder.setMessage(R.string.dialog_fire_missiles) 127 .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { 128 public void onClick(DialogInterface dialog, int id) { 129 // FIRE ZE MISSILES! 130 } 131 }) 132 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 133 public void onClick(DialogInterface dialog, int id) { 134 // User cancelled the dialog 135 } 136 }); 137 // Create the AlertDialog object and return it 138 return builder.create(); 139 } 140 } 141 </pre> 142 143 <div class="figure" style="width:290px;margin:0 0 0 20px"> 144 <img src="{@docRoot}images/ui/dialog_buttons.png" alt="" /> 145 <p class="img-caption"><strong>Hnh 1.</strong> 146 Hp thoi vi mt thng bo v hai nt hnh ng.</p> 147 </div> 148 149 <p>Lc ny, khi bn to mt thc th thuc lp ny v gi {@link 150 android.support.v4.app.DialogFragment#show show()} trn i tng , hp thoi s xut hin 151 nh minh ha trong hnh 1.</p> 152 153 <p>Phn tip theo m t thm v vic s dng cc API {@link android.app.AlertDialog.Builder} 154 to hp thoi.</p> 155 156 <p>Ty vo phc tp ca hp thoi ca bn, bn c th trin khai nhiu loi phng php gi li khc 157 trong {@link android.support.v4.app.DialogFragment}, bao gm tt c 158 <a href="{@docRoot}guide/components/fragments.html#Lifecycle">phng php vng i phn on</a> c bn. 159 160 161 162 163 164 <h2 id="AlertDialog">Xy dng mt Hp thoi Cnh bo</h2> 165 166 167 <p>Lp {@link android.app.AlertDialog} cho php bn xy dng nhiu loi thit k hp thoi v 168 thng l lp hp thoi duy nht m bn s cn. 169 Nh c minh ha trong hnh 2, c ba vng trn mt hp thoi cnh bo:</p> 170 171 <div class="figure" style="width:311px;margin-top:0"> 172 <img src="{@docRoot}images/ui/dialogs_regions.png" alt="" style="margin-bottom:0" /> 173 <p class="img-caption"><strong>Hnh 2.</strong> B tr ca mt hp thoi.</p> 174 </div> 175 176 <ol> 177 <li><b>Tiu </b> 178 <p>Tiu khng bt buc v ch nn c s dng khi vng ni dung 179 b chim bi mt thng bo chi tit, mt danh sch, hay mt b tr ty chnh. Nu bn cn nu 180 mt thng bo hoc cu hi n gin (chng hn nh hp thoi trong hnh 1), bn khng cn tiu .</li> 181 <li><b>Vng ni dung</b> 182 <p>Vng ny c th hin th mt thng bo, danh sch, hay b tr ty chnh khc.</p></li> 183 <li><b>Nt hnh ng</b> 184 <p>S khng c qu ba nt hnh ng trong mt hp thoi.</p></li> 185 </ol> 186 187 <p>Lp {@link android.app.AlertDialog.Builder} 188 cung cp cc API cho php bn to mt {@link android.app.AlertDialog} 189 vi nhng kiu ni dung ny, bao gm mt b tr ty chnh.</p> 190 191 <p> xy dng mt {@link android.app.AlertDialog}:</p> 192 193 <pre> 194 <b>// 1. Instantiate an {@link android.app.AlertDialog.Builder} with its constructor</b> 195 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 196 197 <b>// 2. Chain together various setter methods to set the dialog characteristics</b> 198 builder.setMessage(R.string.dialog_message) 199 .setTitle(R.string.dialog_title); 200 201 <b>// 3. Get the {@link android.app.AlertDialog} from {@link android.app.AlertDialog.Builder#create()}</b> 202 AlertDialog dialog = builder.create(); 203 </pre> 204 205 <p>Cc ch sau cho bit cch nh ngha cc thuc tnh hp thoi khc nhau bng cch 206 s dng lp {@link android.app.AlertDialog.Builder}.</p> 207 208 209 210 211 <h3 id="AddingButtons">Thm nt</h3> 212 213 <p> thm cc nt hnh ng nh trong hnh 2, 214 hy gi cc phng php {@link android.app.AlertDialog.Builder#setPositiveButton setPositiveButton()} v 215 {@link android.app.AlertDialog.Builder#setNegativeButton setNegativeButton()}:</p> 216 217 <pre style="clear:right"> 218 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 219 // Add the buttons 220 builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 221 public void onClick(DialogInterface dialog, int id) { 222 // User clicked OK button 223 } 224 }); 225 builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 226 public void onClick(DialogInterface dialog, int id) { 227 // User cancelled the dialog 228 } 229 }); 230 // Set other dialog properties 231 ... 232 233 // Create the AlertDialog 234 AlertDialog dialog = builder.create(); 235 </pre> 236 237 <p>Cc phng php <code>set...Button()</code> yu cu mt tiu cho nt (c cung cp 238 bi mt <a href="{@docRoot}guide/topics/resources/string-resource.html">ti nguyn xu</a>) v mt 239 {@link android.content.DialogInterface.OnClickListener} c chc nng nh ngha hnh ng s tin hnh 240 khi ngi dng nhn nt.</p> 241 242 <p>C ba nt hnh ng khc nhau m bn c th thm:</p> 243 <dl> 244 <dt>Tch cc</dt> 245 <dd>Bn nn s dng nt ny chp nhn v tip tc vi hnh ng (hnh ng "OK").</dd> 246 <dt>Tiu cc</dt> 247 <dd>Bn nn s dng nt ny hy b hnh ng.</dd> 248 <dt>Trung lp</dt> 249 <dd>Bn nn s dng nt ny khi ngi dng c th khng mun tip tc vi hnh ng, 250 nhng khng hn mun hy b. N nm gia nt 251 tch cc v tiu cc. V d, hnh ng c th l "Nhc ti sau."</dd> 252 </dl> 253 254 <p>Bn ch c th thm mt nt mi loi vo mt {@link 255 android.app.AlertDialog}. Ngha l, bn khng th c nhiu hn mt nt "tch cc".</p> 256 257 258 259 <div class="figure" style="width:290px;margin:0 0 0 40px"> 260 <img src="{@docRoot}images/ui/dialog_list.png" alt="" /> 261 <p class="img-caption"><strong>Hnh 3.</strong> 262 Hp thoi c tiu v danh sch.</p> 263 </div> 264 265 <h3 id="AddingAList">Thm mt danh sch</h3> 266 267 <p>C ba loi danh sch c sn vi cc API {@link android.app.AlertDialog}:</p> 268 <ul> 269 <li>Danh sch mt la chn truyn thng</li> 270 <li>Danh sch mt la chn c nh (nt chn mt)</li> 271 <li>Danh sch nhiu la chn c nh (hp kim)</li> 272 </ul> 273 274 <p> to danh sch mt la chn nh danh sch trong hnh 3, 275 hy s dng phng php {@link android.app.AlertDialog.Builder#setItems setItems()}:</p> 276 277 <pre style="clear:right"> 278 @Override 279 public Dialog onCreateDialog(Bundle savedInstanceState) { 280 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 281 builder.setTitle(R.string.pick_color) 282 .setItems(R.array.colors_array, new DialogInterface.OnClickListener() { 283 public void onClick(DialogInterface dialog, int which) { 284 // The 'which' argument contains the index position 285 // of the selected item 286 } 287 }); 288 return builder.create(); 289 } 290 </pre> 291 292 <p>V danh sch xut hin trong vng ni dung ca hp thoi, 293 hp thoi khng th hin th c thng bo v danh sch v bn nn t mt tiu cho hp thoi 294 bng {@link android.app.AlertDialog.Builder#setTitle setTitle()}. 295 ch nh cc mc cho danh sch, hy gi {@link 296 android.app.AlertDialog.Builder#setItems setItems()}, chuyn mt mng. 297 Hoc, bn c th ch nh mt danh sch bng cch s dng {@link 298 android.app.AlertDialog.Builder#setAdapter setAdapter()}. iu ny cho php bn h tr danh sch 299 bng d liu ng (chng hn nh t mt c s d liu) bng cch s dng {@link android.widget.ListAdapter}.</p> 300 301 <p>Nu bn chn h tr danh sch ca mnh bng mt {@link android.widget.ListAdapter}, 302 hy lun s dng {@link android.support.v4.content.Loader} sao cho ni dung ti 303 khng ng b. iu ny c m t thm trong hng dn 304 <a href="{@docRoot}guide/topics/ui/declaring-layout.html#AdapterViews">Xy dng B tr 305 bng mt Trnh iu hp</a> v <a href="{@docRoot}guide/components/loaders.html">Trnh ti</a> 306 .</p> 307 308 <p class="note"><strong>Lu :</strong> Theo mc nh, chm vo mt mc danh sch s b hp thoi, 309 tr khi bn ang s dng mt trong cc danh sch la chn c nh sau.</p> 310 311 <div class="figure" style="width:290px;margin:-30px 0 0 40px"> 312 <img src="{@docRoot}images/ui/dialog_checkboxes.png" /> 313 <p class="img-caption"><strong>Hnh 4.</strong> 314 Danh sch nhiu mc la chn.</p> 315 </div> 316 317 318 <h4 id="Checkboxes">Thm mt danh sch nhiu la chn hoc mt la chn c nh</h4> 319 320 <p> thm mt danh sch nhiu la chn (hp kim) hoc 321 mt la chn (nt chn mt), hy s dng cc phng php 322 {@link android.app.AlertDialog.Builder#setMultiChoiceItems(Cursor,String,String, 323 DialogInterface.OnMultiChoiceClickListener) setMultiChoiceItems()} hoc 324 {@link android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 325 setSingleChoiceItems()} tng ng.</p> 326 327 <p>V d, sau y l cch bn c th to mt danh sch nhiu la chn nh 328 danh sch c minh ha trong hnh 4 gip lu cc mc 329 c chn trong mt {@link java.util.ArrayList}:</p> 330 331 <pre style="clear:right"> 332 @Override 333 public Dialog onCreateDialog(Bundle savedInstanceState) { 334 mSelectedItems = new ArrayList(); // Where we track the selected items 335 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 336 // Set the dialog title 337 builder.setTitle(R.string.pick_toppings) 338 // Specify the list array, the items to be selected by default (null for none), 339 // and the listener through which to receive callbacks when items are selected 340 .setMultiChoiceItems(R.array.toppings, null, 341 new DialogInterface.OnMultiChoiceClickListener() { 342 @Override 343 public void onClick(DialogInterface dialog, int which, 344 boolean isChecked) { 345 if (isChecked) { 346 // If the user checked the item, add it to the selected items 347 mSelectedItems.add(which); 348 } else if (mSelectedItems.contains(which)) { 349 // Else, if the item is already in the array, remove it 350 mSelectedItems.remove(Integer.valueOf(which)); 351 } 352 } 353 }) 354 // Set the action buttons 355 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 356 @Override 357 public void onClick(DialogInterface dialog, int id) { 358 // User clicked OK, so save the mSelectedItems results somewhere 359 // or return them to the component that opened the dialog 360 ... 361 } 362 }) 363 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 364 @Override 365 public void onClick(DialogInterface dialog, int id) { 366 ... 367 } 368 }); 369 370 return builder.create(); 371 } 372 </pre> 373 374 <p>Mc d c danh sch truyn thng v danh sch c nt chn mt 375 u cung cp hnh ng "mt la chn", bn nn s dng {@link 376 android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 377 setSingleChoiceItems()} nu bn mun c nh la chn ca ngi dng. 378 C th, nu vic m hp thoi li sau ny bo hiu la chn hin ti ca ngi dng, khi 379 bn hy to mt danh sch vi cc nt chn mt.</p> 380 381 382 383 384 385 <h3 id="CustomLayout">To mt B tr Ty chnh</h3> 386 387 <div class="figure" style="width:290px;margin:-30px 0 0 40px"> 388 <img src="{@docRoot}images/ui/dialog_custom.png" alt="" /> 389 <p class="img-caption"><strong>Hnh 5.</strong> Mt b tr hp thoi ty chnh.</p> 390 </div> 391 392 <p>Nu bn mun mt b tr ty chnh trong mt hp thoi, hy to mt b tr v thm n vo mt 393 {@link android.app.AlertDialog} bng cch gi {@link 394 android.app.AlertDialog.Builder#setView setView()} trn i tng {@link 395 android.app.AlertDialog.Builder} ca bn.</p> 396 397 <p>Theo mc nh, b tr ty chnh s lp y ca s hp thoi, nhng bn vn c th 398 s dng cc phng php {@link android.app.AlertDialog.Builder} thm nt v tiu .</p> 399 400 <p>V d, sau y l tp b tr cho hp thoi trong Hnh 5:</p> 401 402 <p style="clear:right" class="code-caption">res/layout/dialog_signin.xml</p> 403 <pre> 404 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 405 android:orientation="vertical" 406 android:layout_width="wrap_content" 407 android:layout_height="wrap_content"> 408 <ImageView 409 android:src="@drawable/header_logo" 410 android:layout_width="match_parent" 411 android:layout_height="64dp" 412 android:scaleType="center" 413 android:background="#FFFFBB33" 414 android:contentDescription="@string/app_name" /> 415 <EditText 416 android:id="@+id/username" 417 android:inputType="textEmailAddress" 418 android:layout_width="match_parent" 419 android:layout_height="wrap_content" 420 android:layout_marginTop="16dp" 421 android:layout_marginLeft="4dp" 422 android:layout_marginRight="4dp" 423 android:layout_marginBottom="4dp" 424 android:hint="@string/username" /> 425 <EditText 426 android:id="@+id/password" 427 android:inputType="textPassword" 428 android:layout_width="match_parent" 429 android:layout_height="wrap_content" 430 android:layout_marginTop="4dp" 431 android:layout_marginLeft="4dp" 432 android:layout_marginRight="4dp" 433 android:layout_marginBottom="16dp" 434 android:fontFamily="sans-serif" 435 android:hint="@string/password"/> 436 </LinearLayout> 437 </pre> 438 439 <p class="note"><strong>Mo:</strong> Theo mc nh, khi bn t mt phn t {@link android.widget.EditText} 440 s dng kiu u vo {@code "textPassword"}, h phng c t thnh n cch, v th 441 bn nn i h phng thnh {@code "sans-serif"} sao cho c hai trng vn bn u s dng 442 mt kiu phng thng nht.</p> 443 444 <p> bung b tr ra trong {@link android.support.v4.app.DialogFragment} ca bn, 445 hy ly mt {@link android.view.LayoutInflater} vi 446 {@link android.app.Activity#getLayoutInflater()} v gi 447 {@link android.view.LayoutInflater#inflate inflate()}, trong tham s u tin 448 l ID ti nguyn b tr v tham s th hai l mt dng xem m cho b tr. 449 Khi , bn c th gi {@link android.app.AlertDialog#setView setView()} 450 t b tr vo mt hp thoi.</p> 451 452 <pre> 453 @Override 454 public Dialog onCreateDialog(Bundle savedInstanceState) { 455 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 456 // Get the layout inflater 457 LayoutInflater inflater = getActivity().getLayoutInflater(); 458 459 // Inflate and set the layout for the dialog 460 // Pass null as the parent view because its going in the dialog layout 461 builder.setView(inflater.inflate(R.layout.dialog_signin, null)) 462 // Add action buttons 463 .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() { 464 @Override 465 public void onClick(DialogInterface dialog, int id) { 466 // sign in the user ... 467 } 468 }) 469 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 470 public void onClick(DialogInterface dialog, int id) { 471 LoginDialogFragment.this.getDialog().cancel(); 472 } 473 }); 474 return builder.create(); 475 } 476 </pre> 477 478 <div class="note"> 479 <p><strong>Mo:</strong> Nu bn mun mt hp thoi ty chnh, 480 thay vo , bn c th hin th {@link android.app.Activity} nh l mt hp thoi 481 thay v s dng cc API {@link android.app.Dialog}. Ch cn to mt hot ng v t ch ca n thnh 482 {@link android.R.style#Theme_Holo_Dialog Theme.Holo.Dialog} 483 trong phn t bn k khai <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 484 <activity>}</a>:</p> 485 486 <pre> 487 <activity android:theme="@android:style/Theme.Holo.Dialog" > 488 </pre> 489 <p>Vy l xong. Lc ny, hot ng s hin th mt ca s hp thoi thay v ton mn hnh.</p> 490 </div> 491 492 493 494 <h2 id="PassingEvents">Chuyn S kin li My ch ca Hp thoi</h2> 495 496 <p>Khi ngi dng chm vo mt trong cc nt hnh ng ca hp thoi hoc chn mt mc t danh sch ca hp thoi, 497 {@link android.support.v4.app.DialogFragment} ca bn c th t thc hin hnh ng 498 cn thit, nhng thng th bn s mun chuyn s kin ti hot ng hoc phn on 499 m hp thoi. lm iu ny, hy nh ngha mt giao din bng mt phng php cho mi loi s kin nhp. 500 Sau , trin khai giao din trong thnh phn ch m s 501 nhn s kin hnh ng t hp thoi.</p> 502 503 <p>V d, sau y l mt {@link android.support.v4.app.DialogFragment} c chc nng nh ngha mt 504 giao din m thng qua , n s chuyn cc s kin li cho hot ng ch:</p> 505 506 <pre> 507 public class NoticeDialogFragment extends DialogFragment { 508 509 /* The activity that creates an instance of this dialog fragment must 510 * implement this interface in order to receive event callbacks. 511 * Each method passes the DialogFragment in case the host needs to query it. */ 512 public interface NoticeDialogListener { 513 public void onDialogPositiveClick(DialogFragment dialog); 514 public void onDialogNegativeClick(DialogFragment dialog); 515 } 516 517 // Use this instance of the interface to deliver action events 518 NoticeDialogListener mListener; 519 520 // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 521 @Override 522 public void onAttach(Activity activity) { 523 super.onAttach(activity); 524 // Verify that the host activity implements the callback interface 525 try { 526 // Instantiate the NoticeDialogListener so we can send events to the host 527 mListener = (NoticeDialogListener) activity; 528 } catch (ClassCastException e) { 529 // The activity doesn't implement the interface, throw exception 530 throw new ClassCastException(activity.toString() 531 + " must implement NoticeDialogListener"); 532 } 533 } 534 ... 535 } 536 </pre> 537 538 <p>Hot ng lu gi hp thoi s to mt thc th ca hp thoi 539 bng hm dng ca phn on hp thoi v nhn s kin 540 ca hp thoi thng qua trin khai giao din {@code NoticeDialogListener}:</p> 541 542 <pre> 543 public class MainActivity extends FragmentActivity 544 implements NoticeDialogFragment.NoticeDialogListener{ 545 ... 546 547 public void showNoticeDialog() { 548 // Create an instance of the dialog fragment and show it 549 DialogFragment dialog = new NoticeDialogFragment(); 550 dialog.show(getSupportFragmentManager(), "NoticeDialogFragment"); 551 } 552 553 // The dialog fragment receives a reference to this Activity through the 554 // Fragment.onAttach() callback, which it uses to call the following methods 555 // defined by the NoticeDialogFragment.NoticeDialogListener interface 556 @Override 557 public void onDialogPositiveClick(DialogFragment dialog) { 558 // User touched the dialog's positive button 559 ... 560 } 561 562 @Override 563 public void onDialogNegativeClick(DialogFragment dialog) { 564 // User touched the dialog's negative button 565 ... 566 } 567 } 568 </pre> 569 570 <p>V hot ng ch s trin khai {@code NoticeDialogListener}—, c 571 thc thi bi phng php gi li {@link android.support.v4.app.Fragment#onAttach onAttach()} 572 minh ha bn trn,—phn on hp thoi c th s dng cc phng php gi li 573 giao din chuyn cc s kin nhp cho hot ng:</p> 574 575 <pre> 576 public class NoticeDialogFragment extends DialogFragment { 577 ... 578 579 @Override 580 public Dialog onCreateDialog(Bundle savedInstanceState) { 581 // Build the dialog and set up the button click handlers 582 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 583 builder.setMessage(R.string.dialog_fire_missiles) 584 .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { 585 public void onClick(DialogInterface dialog, int id) { 586 // Send the positive button event back to the host activity 587 mListener.onDialogPositiveClick(NoticeDialogFragment.this); 588 } 589 }) 590 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 591 public void onClick(DialogInterface dialog, int id) { 592 // Send the negative button event back to the host activity 593 mListener.onDialogNegativeClick(NoticeDialogFragment.this); 594 } 595 }); 596 return builder.create(); 597 } 598 } 599 </pre> 600 601 602 603 <h2 id="ShowingADialog">Hin th mt Hp thoi</h2> 604 605 <p>Khi bn mun hin th hp thoi ca mnh, hy to mt thc th {@link 606 android.support.v4.app.DialogFragment} ca bn v gi {@link android.support.v4.app.DialogFragment#show 607 show()}, chuyn {@link android.support.v4.app.FragmentManager} v mt tn tag 608 cho phn on hp thoi.</p> 609 610 <p>Bn c th nhn c {@link android.support.v4.app.FragmentManager} bng cch gi 611 {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()} t 612 {@link android.support.v4.app.FragmentActivity} hoc {@link 613 android.support.v4.app.Fragment#getFragmentManager()} t mt {@link 614 android.support.v4.app.Fragment}. V d:</p> 615 616 <pre> 617 public void confirmFireMissiles() { 618 DialogFragment newFragment = new FireMissilesDialogFragment(); 619 newFragment.show(getSupportFragmentManager(), "missiles"); 620 } 621 </pre> 622 623 <p>Tham i th hai, {@code "missiles"}, l mt tn tag duy nht m h thng s dng lu 624 v khi phc trng thi ca phn on khi cn thit. Tag cng cho php bn nhn mt iu khin (handle) cho 625 phn on bng cch gi {@link android.support.v4.app.FragmentManager#findFragmentByTag 626 findFragmentByTag()}.</p> 627 628 629 630 631 <h2 id="FullscreenDialog">Hin th mt Hp thoi Ton mn hnh hoc dng mt Phn on Nhng</h2> 632 633 <p>Bn c th c mt thit k UI m trong bn mun mt phn UI xut hin nh mt hp thoi trong mt s 634 tnh hung, nhng di dng ton mn hnh hoc phn on nhng trong trng hp khc (c th ph thuc 635 vo thit b l mn hnh ln hay nh). Lp {@link android.support.v4.app.DialogFragment} 636 cung cp cho bn s linh hot ny v n vn c th ng vai tr nh mt {@link 637 android.support.v4.app.Fragment} nhng c.</p> 638 639 <p>Tuy nhin, bn khng th s dng {@link android.app.AlertDialog.Builder AlertDialog.Builder} 640 hay cc i tng {@link android.app.Dialog} khc xy dng hp thoi trong trng hp ny. Nu 641 bn mun {@link android.support.v4.app.DialogFragment} c th 642 nhng c, bn phi nh ngha UI ca hp thoi trong mt b tr, ri ti b tr trong lnh gi li 643 {@link android.support.v4.app.DialogFragment#onCreateView 644 onCreateView()}.</p> 645 646 <p>Sau y l mt v d {@link android.support.v4.app.DialogFragment} c th xut hin nh mt 647 hp thoi hoc phn on nhng c (s dng mt b tr c tn gi <code>purchase_items.xml</code>):</p> 648 649 <pre> 650 public class CustomDialogFragment extends DialogFragment { 651 /** The system calls this to get the DialogFragment's layout, regardless 652 of whether it's being displayed as a dialog or an embedded fragment. */ 653 @Override 654 public View onCreateView(LayoutInflater inflater, ViewGroup container, 655 Bundle savedInstanceState) { 656 // Inflate the layout to use as dialog or embedded fragment 657 return inflater.inflate(R.layout.purchase_items, container, false); 658 } 659 660 /** The system calls this only when creating the layout in a dialog. */ 661 @Override 662 public Dialog onCreateDialog(Bundle savedInstanceState) { 663 // The only reason you might override this method when using onCreateView() is 664 // to modify any dialog characteristics. For example, the dialog includes a 665 // title by default, but your custom layout might not need it. So here you can 666 // remove the dialog title, but you must call the superclass to get the Dialog. 667 Dialog dialog = super.onCreateDialog(savedInstanceState); 668 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 669 return dialog; 670 } 671 } 672 </pre> 673 674 <p>V sau y l mt s m quyt nh xem hin th phn on nh mt hp thoi 675 hay UI ton mn hnh, da vo kch c mn hnh:</p> 676 677 <pre> 678 public void showDialog() { 679 FragmentManager fragmentManager = getSupportFragmentManager(); 680 CustomDialogFragment newFragment = new CustomDialogFragment(); 681 682 if (mIsLargeLayout) { 683 // The device is using a large layout, so show the fragment as a dialog 684 newFragment.show(fragmentManager, "dialog"); 685 } else { 686 // The device is smaller, so show the fragment fullscreen 687 FragmentTransaction transaction = fragmentManager.beginTransaction(); 688 // For a little polish, specify a transition animation 689 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 690 // To make it fullscreen, use the 'content' root view as the container 691 // for the fragment, which is always the root view for the activity 692 transaction.add(android.R.id.content, newFragment) 693 .addToBackStack(null).commit(); 694 } 695 } 696 </pre> 697 698 <p> bit thm thng tin v vic thc hin cc giao tc phn on, hy xem hng dn 699 <a href="{@docRoot}guide/components/fragments.html">Phn on</a>.</p> 700 701 <p>Trong v d ny, boolean <code>mIsLargeLayout</code> ch nh liu thit b hin ti 702 c nn s dng thit k b tr ln ca ng dng (v v th, n hin th phn on ny nh mt hp thoi thay v 703 ton mn hnh) hay khng. Cch tt nht t loi boolean ny l khai bo mt 704 <a href="{@docRoot}guide/topics/resources/more-resources.html#Bool">gi tr ti nguyn bool</a> 705 bng mt gi tr <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">ti nguyn thay th</a> cho cc kch c mn hnh khc nhau. V d, sau y l hai 706 phin bn ca ti nguyn bool cho cc kch c mn hnh khc nhau:</p> 707 708 <p class="code-caption">res/values/bools.xml</p> 709 <pre> 710 <!-- Default boolean values --> 711 <resources> 712 <bool name="large_layout">false</bool> 713 </resources> 714 </pre> 715 716 <p class="code-caption">res/values-large/bools.xml</p> 717 <pre> 718 <!-- Large screen boolean values --> 719 <resources> 720 <bool name="large_layout">true</bool> 721 </resources> 722 </pre> 723 724 <p>Khi , bn c th khi to gi tr {@code mIsLargeLayout} trong phng php 725 {@link android.app.Activity#onCreate onCreate()} ca hot ng:</p> 726 727 <pre> 728 boolean mIsLargeLayout; 729 730 @Override 731 public void onCreate(Bundle savedInstanceState) { 732 super.onCreate(savedInstanceState); 733 setContentView(R.layout.activity_main); 734 735 mIsLargeLayout = getResources().getBoolean(R.bool.large_layout); 736 } 737 </pre> 738 739 740 741 <h3 id="ActivityAsDialog">Hin th mt hot ng di dng mt hp thoi trn mn hnh ln</h3> 742 743 <p>Thay v hin th mt hp thoi thnh UI ton mn hnh trn cc mn hnh nh, bn c th t c 744 kt qu tng t bng cch hin th mt {@link android.app.Activity} thnh mt hp thoi trn 745 mn hnh ln. Phng php m bn chn ph thuc vo thit k ng dng ca bn, nhng 746 vic hin th mt hot ng thnh mt hp thoi thng c ch khi ng dng ca bn c thit k cho mn hnh 747 nh v bn mun ci thin tri nghim trn my tnh bng bng cch hin th mt hot ng c vng i ngn 748 thnh mt hp thoi.</p> 749 750 <p> hin th mt hot ng thnh mt hp thoi ch khi trn mn hnh ln, 751 hy p dng ch {@link android.R.style#Theme_Holo_DialogWhenLarge Theme.Holo.DialogWhenLarge} 752 cho phn t bn k khai <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 753 <activity>}</a>:</p> 754 755 <pre> 756 <activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" > 757 </pre> 758 759 <p> bit thm thng tin v vic to kiu cho cc hot ng ca bn bng ch , hy xem hng dn <a href="{@docRoot}guide/topics/ui/themes.html">Kiu v Ch </a>.</p> 760 761 762 763 <h2 id="DismissingADialog">B mt Hp thoi</h2> 764 765 <p>Khi ngi dng chm vo bt k nt hnh ng no c to bng 766 {@link android.app.AlertDialog.Builder}, h thng s b hp thoi cho bn.</p> 767 768 <p>H thng cng b hp thoi khi ngi dng chm vo mt mc trong mt danh sch hp thoi, tr 769 khi danh sch s dng nt chn mt hoc hp kim. Nu khng, bn c th b th cng hp thoi ca mnh 770 bng cch gi {@link android.support.v4.app.DialogFragment#dismiss()} trn {@link 771 android.support.v4.app.DialogFragment} ca bn.</p> 772 773 <p>Trong trng hp bn cn thc hin cc 774 hnh ng nht nh khi hp thoi bin mt, bn c th trin khai phng php {@link 775 android.support.v4.app.DialogFragment#onDismiss onDismiss()} trong {@link 776 android.support.v4.app.DialogFragment} ca mnh.</p> 777 778 <p>Bn cng c th <em>hy b</em> mt hp thoi. y l mt s kin c bit ch bo ngi dng 779 ch ri khi hp thoi m khng hon thnh tc v. iu ny xy ra nu ngi dng nhn nt 780 <em>Quay li</em>, chm vo mn hnh ngoi vng hp thoi, 781 hoc nu bn cng khai gi {@link android.app.Dialog#cancel()} trn {@link 782 android.app.Dialog} (chng hn nh khi hi p li mt nt "Hy b" trong hp thoi).</p> 783 784 <p>Nh nu trong v d bn trn, bn c th hi p li s kin hy b ny bng cch trin khai 785 {@link android.support.v4.app.DialogFragment#onCancel onCancel()} trong lp {@link 786 android.support.v4.app.DialogFragment} ca mnh.</p> 787 788 <p class="note"><strong>Lu :</strong> H thng s gi 789 {@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} trn mi s kin m 790 gi ra lnh gi li {@link android.support.v4.app.DialogFragment#onCancel onCancel()}. Tuy nhin, 791 nu bn gi {@link android.app.Dialog#dismiss Dialog.dismiss()} hoc {@link 792 android.support.v4.app.DialogFragment#dismiss DialogFragment.dismiss()}, 793 h thng s gi {@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} <em>ch 794 khng phi</em> {@link android.support.v4.app.DialogFragment#onCancel onCancel()}. V th, nhn chung bn nn 795 gi {@link android.support.v4.app.DialogFragment#dismiss dismiss()} khi ngi dng nhn nt 796 <em>tch cc</em> trong hp thoi ca bn xa hp thoi khi dng xem.</p> 797 798 799