blob: 1fa45508ef0c384df4fec3e132a7029ec840992e [file] [log] [blame]
page.title=Hp thoi
page.tags=alertdialog,dialogfragment
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>Trong tài liu này</h2>
<ol>
<li><a href="#DialogFragment">To mt Phân đon Hp thoi</a></li>
<li><a href="#AlertDialog">Xây dng mt Hp thoi Cnh báo</a>
<ol>
<li><a href="#AddingButtons">Thêm nút</a></li>
<li><a href="#AddingAList">Thêm mt danh sách</a></li>
<li><a href="#CustomLayout">To mt B trí Tùy chnh</a></li>
</ol>
</li>
<li><a href="#PassingEvents">Chuyn S kin li Máy ch ca Hp thoi</a></li>
<li><a href="#ShowingADialog">Hin th mt Hp thoi</a></li>
<li><a href="#FullscreenDialog">Hin th mt Hp thoi Toàn màn hình hoc dng mt Phân đon Nhúng</a>
<ol>
<li><a href="#ActivityAsDialog">Hin th mt hot động dưới dng mt hp thoi trên màn hình ln</a></li>
</ol>
</li>
<li><a href="#DismissingADialog">B mt Hp thoi</a></li>
</ol>
<h2>Lp khóa</h2>
<ol>
<li>{@link android.app.DialogFragment}</li>
<li>{@link android.app.AlertDialog}</li>
</ol>
<h2>Xem thêm</h2>
<ol>
<li><a href="{@docRoot}design/building-blocks/dialogs.html">Hướng dn thiết kế hp thoi</a></li>
<li><a href="{@docRoot}guide/topics/ui/controls/pickers.html">B chn</a> (Hộp thoại Ngày/Giờ)</li>
</ol>
</div>
</div>
<p>Hp thoi là mt ca s nh có chc năng nhc người dùng
đưa ra mt quyết định hoc nhp thông tin b sung. Hp thoi không lp kín màn hình và
thường được s dng cho các s kin mô thái yêu cu người dùng phi thc hin mt hành động trước khi có th đi tiếp.</p>
<div class="note design">
<p><strong>Thiết kế Hp thoi</strong></p>
<p>Để biết thông tin v cách thiết kế hp thoi ca bn, bao gm các đề xut
v ngôn ngữ, hãy đọc hướng dn thiết kế <a href="{@docRoot}design/building-blocks/dialogs.html">Hp thoi</a>.</p>
</div>
<img src="{@docRoot}images/ui/dialogs.png" />
<p>Lp {@link android.app.Dialog} là lp cơ s cho hp thoi, nhưng bn
nên tránh khi to {@link android.app.Dialog} mt cách trc tiếp.
Thay vào đó, hãy s dng mt trong các lp con sau:</p>
<dl>
<dt>{@link android.app.AlertDialog}</dt>
<dd>Hp thoi có th hin th mt tiêu đề, ti đa ba nút, mt danh sách
các mc có th chn, hoc mt b trí tùy chnh.</dd>
<dt>{@link android.app.DatePickerDialog} hoc {@link android.app.TimePickerDialog}</dt>
<dd>Hp thoi vi mt UI được xác định trước, cho phép người dùng chn ngày hoc giờ.</dd>
</dl>
<div class="sidebox">
<h2>Tránh ProgressDialog</h2>
<p>Android có mt lp hp thoi khác gi là
{@link android.app.ProgressDialog}, nó hin th mt hp thoi vi mt thanh tiến độ. Tuy nhiên, nếu bn
cn ch báo tiến độ ti hoc chưa xác định, thay vào đó, bn nên tuân theo hướng dn
thiết kế dành cho <a href="{@docRoot}design/building-blocks/progress.html">Tiến độ &amp;
Hot động</a> và sử dụng một {@link android.widget.ProgressBar} trong bố trí của mình.</p>
</div>
<p>Nhng lp này định nghĩa kiu và cu trúc cho hp thoi ca bn, nhưng bn nên
s dng mt {@link android.support.v4.app.DialogFragment} làm b cha cho hp thoi ca mình.
Lp {@link android.support.v4.app.DialogFragment} s cung cp tt c điu khin mà
bn cn để to hp thoi ca mình và qun lý din mo ca hp thoi, thay vì gi ra các phương pháp
trên đối tượng {@link android.app.Dialog}.</p>
<p>Vic s dng {@link android.support.v4.app.DialogFragment} để qun lý hp thoi
s đảm bo rng nó x lý đúng các s kin vòng đời
chng hn như khi người dùng nhn nút <em>Quay li</em> hoc xoay màn hình. Lp {@link
android.support.v4.app.DialogFragment} cũng cho phép bn s dng li UI ca hp thoi như mt
thành phn có th nhúng trong mt UI rng hơn, ging như mt {@link
android.support.v4.app.Fragment} truyn thng (chng hn như khi bn mun UI hp thoi xut hin khác đi
trên các màn hình ln và nhỏ).</p>
<p>Các phn sau trong hướng dn này mô t cách s dng {@link
android.support.v4.app.DialogFragment} kết hp vi mt đối tượng {@link android.app.AlertDialog}
. Nếu mun to mt b chn ngày hoc giờ, thay vào đó, bn nên đọc hướng dn
<a href="{@docRoot}guide/topics/ui/controls/pickers.html">B chn</a>.</p>
<p class="note"><strong>Lưu ý:</strong>
Vì lp {@link android.app.DialogFragment} ban đầu được b sung cùng vi
Android 3.0 (API mc 11), tài liu này mô t cách s dng lp {@link
android.support.v4.app.DialogFragment} được cung cp kèm <a href="{@docRoot}tools/support-library/index.html">Thư vin H trợ</a>. Bng cách thêm thư vin này
vào ng dng ca mình, bn có th s dng {@link android.support.v4.app.DialogFragment} và nhiu loi
API khác trên các thiết b chy Android 1.6 hoc cao hơn. Nếu phiên bn ti thiu mà ng dng ca bn h tr
là API mc 11 hoc cao hơn, khi đó bn có th s dng phiên bn khuôn kh ca {@link
android.app.DialogFragment}, nhưng hãy chú ý rng các liên kết trong tài liu này dành cho các API
thư vin h trợ. Khi s dng thư vin h trợ,
hãy nh rng bn nhp lp <code>android.support.v4.app.DialogFragment</code>
ch <em>không phi</em> <code>android.app.DialogFragment</code>.</p>
<h2 id="DialogFragment">To mt Phân đon Hp thoi</h2>
<p>Bn có th hoàn thành nhiu loi thiết kế hp thoi&mdash;bao gm
b trí tùy chnh và nhng b trí được mô t trong hướng dn thiết kế <a href="{@docRoot}design/building-blocks/dialogs.html">Hp thoi</a>
&mdash;bng cách m rng
{@link android.support.v4.app.DialogFragment} và to mt {@link android.app.AlertDialog}
trong phương pháp gi li {@link android.support.v4.app.DialogFragment#onCreateDialog
onCreateDialog()}.</p>
<p>Ví dụ, sau đây là mt {@link android.app.AlertDialog} cơ bn được qun lý bên trong
mt {@link android.support.v4.app.DialogFragment}:</p>
<pre>
public class FireMissilesDialogFragment extends DialogFragment {
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
</pre>
<div class="figure" style="width:290px;margin:0 0 0 20px">
<img src="{@docRoot}images/ui/dialog_buttons.png" alt="" />
<p class="img-caption"><strong>Hình 1.</strong>
Hp thoi vi mt thông báo và hai nút hành động.</p>
</div>
<p>Lúc này, khi bn to mt thc th thuc lp này và gi {@link
android.support.v4.app.DialogFragment#show show()} trên đối tượng đó, hộp thoại sẽ xuất hiện
như minh ha trong hình 1.</p>
<p>Phn tiếp theo mô t thêm v vic s dng các API {@link android.app.AlertDialog.Builder}
để to hp thoi.</p>
<p>Tùy vào độ phc tp ca hp thoi ca bn, bn có th trin khai nhiu loi phương pháp gi li khác
trong {@link android.support.v4.app.DialogFragment}, bao gm tt c
<a href="{@docRoot}guide/components/fragments.html#Lifecycle">phương pháp vòng đời phân đon</a> cơ bn.
<h2 id="AlertDialog">Xây dng mt Hp thoi Cnh báo</h2>
<p>Lp {@link android.app.AlertDialog} cho phép bn xây dng nhiu loi thiết kế hp thoi và
thường là lp hp thoi duy nht mà bn s cn.
Như được minh ha trong hình 2, có ba vùng trên mt hp thoi cnh báo:</p>
<div class="figure" style="width:311px;margin-top:0">
<img src="{@docRoot}images/ui/dialogs_regions.png" alt="" style="margin-bottom:0" />
<p class="img-caption"><strong>Hình 2.</strong> Bố trí của một hộp thoại.</p>
</div>
<ol>
<li><b>Tiêu đề</b>
<p>Tiêu đề không bt buc và ch nên được s dng khi vùng ni dung
b chiếm bi mt thông báo chi tiết, mt danh sách, hay mt b trí tùy chnh. Nếu bn cn nêu
mt thông báo hoc câu hi đơn gin (chng hn như hp thoi trong hình 1), bn không cn tiêu đề.</li>
<li><b>Vùng ni dung</b>
<p>Vùng này có th hin th mt thông báo, danh sách, hay b trí tùy chnh khác.</p></li>
<li><b>Nút hành động</b>
<p>S không có quá ba nút hành động trong mt hp thoi.</p></li>
</ol>
<p>Lp {@link android.app.AlertDialog.Builder}
cung cp các API cho phép bn to mt {@link android.app.AlertDialog}
vi nhng kiu ni dung này, bao gm mt b trí tùy chnh.</p>
<p>Để xây dng mt {@link android.app.AlertDialog}:</p>
<pre>
<b>// 1. Instantiate an {@link android.app.AlertDialog.Builder} with its constructor</b>
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
<b>// 2. Chain together various setter methods to set the dialog characteristics</b>
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
<b>// 3. Get the {@link android.app.AlertDialog} from {@link android.app.AlertDialog.Builder#create()}</b>
AlertDialog dialog = builder.create();
</pre>
<p>Các ch đề sau cho biết cách định nghĩa các thuc tính hp thoi khác nhau bng cách
s dng lp {@link android.app.AlertDialog.Builder}.</p>
<h3 id="AddingButtons">Thêm nút</h3>
<p>Để thêm các nút hành động như trong hình 2,
hãy gi các phương pháp {@link android.app.AlertDialog.Builder#setPositiveButton setPositiveButton()} và
{@link android.app.AlertDialog.Builder#setNegativeButton setNegativeButton()}:</p>
<pre style="clear:right">
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
</pre>
<p>Các phương pháp <code>set...Button()</code> yêu cu mt tiêu đề cho nút (được cung cp
bi mt <a href="{@docRoot}guide/topics/resources/string-resource.html">tài nguyên xâu</a>) và mt
{@link android.content.DialogInterface.OnClickListener} có chc năng định nghĩa hành động s tiến hành
khi người dùng nhn nút.</p>
<p>Có ba nút hành động khác nhau mà bn có th thêm:</p>
<dl>
<dt>Tích cc</dt>
<dd>Bn nên s dng nút này để chp nhn và tiếp tc vi hành động (hành động "OK").</dd>
<dt>Tiêu cc</dt>
<dd>Bn nên s dng nút này để hy b hành động.</dd>
<dt>Trung lp</dt>
<dd>Bn nên s dng nút này khi người dùng có th không mun tiếp tc vi hành động,
nhưng không hn mun hy bỏ. Nó nm gia nút
tích cc và tiêu cc. Ví dụ, hành động có th là "Nhắc tôi sau."</dd>
</dl>
<p>Bn ch có th thêm mt nút mi loi vào mt {@link
android.app.AlertDialog}. Nghĩa là, bn không th có nhiu hơn mt nút "tích cực".</p>
<div class="figure" style="width:290px;margin:0 0 0 40px">
<img src="{@docRoot}images/ui/dialog_list.png" alt="" />
<p class="img-caption"><strong>Hình 3.</strong>
Hp thoi có tiêu đề và danh sách.</p>
</div>
<h3 id="AddingAList">Thêm mt danh sách</h3>
<p>Có ba loi danh sách có sn vi các API {@link android.app.AlertDialog}:</p>
<ul>
<li>Danh sách mt la chn truyn thng</li>
<li>Danh sách mt la chn c định (nút chn mt)</li>
<li>Danh sách nhiu la chn c định (hp kim)</li>
</ul>
<p>Để to danh sách mt la chn như danh sách trong hình 3,
hãy s dng phương pháp {@link android.app.AlertDialog.Builder#setItems setItems()}:</p>
<pre style="clear:right">
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
</pre>
<p>Vì danh sách xut hin trong vùng ni dung ca hp thoi,
hp thoi không th hin th c thông báo và danh sách và bn nên đặt mt tiêu đề cho hp thoi
bng {@link android.app.AlertDialog.Builder#setTitle setTitle()}.
Để ch định các mc cho danh sách, hãy gi {@link
android.app.AlertDialog.Builder#setItems setItems()}, chuyển một mảng.
Hoc, bn có th ch định mt danh sách bng cách s dng {@link
android.app.AlertDialog.Builder#setAdapter setAdapter()}. Điều này cho phép bạn hỗ trợ danh sách
bng d liu động (chng hn như t mt cơ s d liu) bng cách s dng {@link android.widget.ListAdapter}.</p>
<p>Nếu bn chn h tr danh sách ca mình bng mt {@link android.widget.ListAdapter},
hãy luôn s dng {@link android.support.v4.content.Loader} sao cho ni dung ti
không đồng bộ. Điu này được mô t thêm trong hướng dn
<a href="{@docRoot}guide/topics/ui/declaring-layout.html#AdapterViews">Xây dng B trí
bng mt Trình điu hp</a> và <a href="{@docRoot}guide/components/loaders.html">Trình tải</a>
.</p>
<p class="note"><strong>Lưu ý:</strong> Theo mặc định, chạm vào một mục danh sách sẽ bỏ hộp thoại,
trừ khi bạn đang sử dụng một trong các danh sách lựa chọn cố định sau.</p>
<div class="figure" style="width:290px;margin:-30px 0 0 40px">
<img src="{@docRoot}images/ui/dialog_checkboxes.png" />
<p class="img-caption"><strong>Hình 4.</strong>
Danh sách nhiều mục lựa chọn.</p>
</div>
<h4 id="Checkboxes">Thêm một danh sách nhiều lựa chọn hoặc một lựa chọn cố định</h4>
<p>Để thêm một danh sách nhiều lựa chọn (hộp kiểm) hoặc
một lựa chọn (nút chọn một), hãy sử dụng các phương pháp
{@link android.app.AlertDialog.Builder#setMultiChoiceItems(Cursor,String,String,
DialogInterface.OnMultiChoiceClickListener) setMultiChoiceItems()} hoặc
{@link android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener)
setSingleChoiceItems()} tương ứng.</p>
<p>Ví dụ, sau đây là cách bạn có thể tạo một danh sách nhiều lựa chọn như
danh sách được minh họa trong hình 4 giúp lưu các mục
được chọn trong một {@link java.util.ArrayList}:</p>
<pre style="clear:right">
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.pick_toppings)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
&#64;Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
&#64;Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
&#64;Override
public void onClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
}
</pre>
<p>Mặc dù cả danh sách truyền thống và danh sách có nút chọn một
đều cung cấp hành động "mt la chn", bạn nên sử dụng {@link
android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener)
setSingleChoiceItems()} nếu bạn muốn cố định lựa chọn của người dùng.
Cụ thể, nếu việc mở hộp thoại lại sau này báo hiệu lựa chọn hiện tại của người dùng, khi đó
bạn hãy tạo một danh sách với các nút chọn một.</p>
<h3 id="CustomLayout">Tạo một Bố trí Tùy chỉnh</h3>
<div class="figure" style="width:290px;margin:-30px 0 0 40px">
<img src="{@docRoot}images/ui/dialog_custom.png" alt="" />
<p class="img-caption"><strong>Hình 5.</strong> Một bố trí hộp thoại tùy chỉnh.</p>
</div>
<p>Nếu bạn muốn một bố trí tùy chỉnh trong một hộp thoại, hãy tạo một bố trí và thêm nó vào một
{@link android.app.AlertDialog} bằng cách gọi {@link
android.app.AlertDialog.Builder#setView setView()} trên đối tượng {@link
android.app.AlertDialog.Builder} của bạn.</p>
<p>Theo mặc định, bố trí tùy chỉnh sẽ lấp đầy cửa sổ hộp thoại, nhưng bạn vẫn có thể
sử dụng các phương pháp {@link android.app.AlertDialog.Builder} để thêm nút và tiêu đề.</p>
<p>Ví dụ, sau đây là tệp bố trí cho hộp thoại trong Hình 5:</p>
<p style="clear:right" class="code-caption">res/layout/dialog_signin.xml</p>
<pre>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
&lt;ImageView
android:src="@drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
&lt;EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
&lt;EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
&lt;/LinearLayout>
</pre>
<p class="note"><strong>Mo:</strong> Theo mc định, khi bn đặt mt phn t {@link android.widget.EditText}
để s dng kiu đầu vào {@code "textPassword"}, h phông được đặt thành đơn cách, vì thế
bn nên đổi h phông thành {@code "sans-serif"} sao cho c hai trường văn bn đều s dng
mt kiu phông thng nht.</p>
<p>Để bung b trí ra trong {@link android.support.v4.app.DialogFragment} ca bn,
hãy ly mt {@link android.view.LayoutInflater} vi
{@link android.app.Activity#getLayoutInflater()} và gọi
{@link android.view.LayoutInflater#inflate inflate()}, trong đó tham số đầu tiên
là ID tài nguyên b trí và tham s th hai là mt dng xem m cho b trí.
Khi đó, bn có th gi {@link android.app.AlertDialog#setView setView()}
để đặt b trí vào mt hp thoi.</p>
<pre>
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
&#64;Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
</pre>
<div class="note">
<p><strong>Mo:</strong> Nếu bn mun mt hp thoi tùy chnh,
thay vào đó, bn có th hin th {@link android.app.Activity} như là mt hp thoi
thay vì s dng các API {@link android.app.Dialog}. Ch cn to mt hot động và đặt ch đề ca nó thành
{@link android.R.style#Theme_Holo_Dialog Theme.Holo.Dialog}
trong phn t bn kê khai <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
&lt;activity&gt;}</a>:</p>
<pre>
&lt;activity android:theme="&#64;android:style/Theme.Holo.Dialog" >
</pre>
<p>Vy là xong. Lúc này, hot động s hin th mt ca s hp thoi thay vì toàn màn hình.</p>
</div>
<h2 id="PassingEvents">Chuyn S kin li Máy ch ca Hp thoi</h2>
<p>Khi người dùng chm vào mt trong các nút hành động ca hp thoi hoc chn mt mc t danh sách ca hp thoi,
{@link android.support.v4.app.DialogFragment} ca bn có th t thc hin hành động
cn thiết, nhưng thường thì bn s mun chuyn s kin ti hot động hoc phân đon
đã m hp thoi. Để làm điu này, hãy định nghĩa mt giao din bng mt phương pháp cho mi loi s kin nhp.
Sau đó, trin khai giao din đó trong thành phn ch mà s
nhn s kin hành động t hp thoi.</p>
<p>Ví dụ, sau đây là mt {@link android.support.v4.app.DialogFragment} có chc năng định nghĩa mt
giao din mà thông qua đó, nó s chuyn các s kin li cho hot động chủ:</p>
<pre>
public class NoticeDialogFragment extends DialogFragment {
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
&#64;Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
...
}
</pre>
<p>Hot động lưu gi hp thoi s to mt thc th ca hp thoi
bng hàm dng ca phân đon hp thoi và nhn s kin
ca hp thoi thông qua trin khai giao din {@code NoticeDialogListener}:</p>
<pre>
public class MainActivity extends FragmentActivity
implements NoticeDialogFragment.NoticeDialogListener{
...
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
&#64;Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
...
}
&#64;Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
...
}
}
</pre>
<p>Vì hot động ch s trin khai {@code NoticeDialogListener}&mdash;, được
thc thi bi phương pháp gi li {@link android.support.v4.app.Fragment#onAttach onAttach()}
minh ha bên trên,&mdash;phân đon hp thoi có th s dng các phương pháp gi li
giao din để chuyn các s kin nhp cho hot động:</p>
<pre>
public class NoticeDialogFragment extends DialogFragment {
...
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
return builder.create();
}
}
</pre>
<h2 id="ShowingADialog">Hin th mt Hp thoi</h2>
<p>Khi bn mun hin th hp thoi ca mình, hãy to mt thc th {@link
android.support.v4.app.DialogFragment} ca bn và gi {@link android.support.v4.app.DialogFragment#show
show()}, chuyn {@link android.support.v4.app.FragmentManager} và mt tên tag
cho phân đon hp thoi.</p>
<p>Bn có th nhn được {@link android.support.v4.app.FragmentManager} bng cách gi
{@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()} từ
{@link android.support.v4.app.FragmentActivity} hoc {@link
android.support.v4.app.Fragment#getFragmentManager()} từ một {@link
android.support.v4.app.Fragment}. Ví dụ:</p>
<pre>
public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
</pre>
<p>Tham đối th hai, {@code "missiles"}, là mt tên tag duy nht mà h thng s dng để lưu
và khôi phc trng thái ca phân đon khi cn thiết. Tag cũng cho phép bn nhn mt điu khin (handle) cho
phân đon bng cách gi {@link android.support.v4.app.FragmentManager#findFragmentByTag
findFragmentByTag()}.</p>
<h2 id="FullscreenDialog">Hin th mt Hp thoi Toàn màn hình hoc dng mt Phân đon Nhúng</h2>
<p>Bn có th có mt thiết kế UI mà trong đó bn mun mt phn UI xut hin như mt hp thoi trong mt s
tình hung, nhưng dưới dng toàn màn hình hoc phân đon nhúng trong trường hp khác (có th ph thuc
vào thiết b là màn hình ln hay nhỏ). Lp {@link android.support.v4.app.DialogFragment}
cung cp cho bn s linh hot này vì nó vn có th đóng vai trò như mt {@link
android.support.v4.app.Fragment} nhúng được.</p>
<p>Tuy nhiên, bn không th s dng {@link android.app.AlertDialog.Builder AlertDialog.Builder}
hay các đối tượng {@link android.app.Dialog} khác để xây dng hp thoi trong trường hp này. Nếu
bn mun {@link android.support.v4.app.DialogFragment} có th
nhúng được, bn phi định nghĩa UI ca hp thoi trong mt b trí, ri ti b trí đó trong lnh gi li
{@link android.support.v4.app.DialogFragment#onCreateView
onCreateView()}.</p>
<p>Sau đây là mt ví d {@link android.support.v4.app.DialogFragment} có th xut hin như mt
hp thoi hoc phân đon nhúng được (s dng mt b trí có tên gi <code>purchase_items.xml</code>):</p>
<pre>
public class CustomDialogFragment extends DialogFragment {
/** The system calls this to get the DialogFragment's layout, regardless
of whether it's being displayed as a dialog or an embedded fragment. */
&#64;Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.purchase_items, container, false);
}
/** The system calls this only when creating the layout in a dialog. */
&#64;Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
</pre>
<p>Và sau đây là mt s mã quyết định xem hin th phân đon như mt hp thoi
hay UI toàn màn hình, da vào kích c màn hình:</p>
<pre>
public void showDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();
if (mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
newFragment.show(fragmentManager, "dialog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
}
</pre>
<p>Để biết thêm thông tin v vic thc hin các giao tác phân đon, hãy xem hướng dn
<a href="{@docRoot}guide/components/fragments.html">Phân đon</a>.</p>
<p>Trong ví d này, boolean <code>mIsLargeLayout</code> ch định liu thiết b hin ti
có nên s dng thiết kế b trí ln ca ng dng (và vì thế, nó hin th phân đon này như mt hp thoi thay vì
toàn màn hình) hay không. Cách tt nht để đặt loi boolean này đó là khai báo mt
<a href="{@docRoot}guide/topics/resources/more-resources.html#Bool">giá tr tài nguyên bool</a>
bng mt giá tr <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">tài nguyên thay thế</a> cho các kích c màn hình khác nhau. Ví dụ, sau đây là hai
phiên bn ca tài nguyên bool cho các kích c màn hình khác nhau:</p>
<p class="code-caption">res/values/bools.xml</p>
<pre>
&lt;!-- Default boolean values -->
&lt;resources>
&lt;bool name="large_layout">false&lt;/bool>
&lt;/resources>
</pre>
<p class="code-caption">res/values-large/bools.xml</p>
<pre>
&lt;!-- Large screen boolean values -->
&lt;resources>
&lt;bool name="large_layout">true&lt;/bool>
&lt;/resources>
</pre>
<p>Khi đó, bn có th khi to giá tr {@code mIsLargeLayout} trong phương pháp
{@link android.app.Activity#onCreate onCreate()} của hoạt động:</p>
<pre>
boolean mIsLargeLayout;
&#64;Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
}
</pre>
<h3 id="ActivityAsDialog">Hin th mt hot động dưới dng mt hp thoi trên màn hình ln</h3>
<p>Thay vì hin th mt hp thoi thành UI toàn màn hình trên các màn hình nhỏ, bn có th đạt được
kết qu tương t bng cách hin th mt {@link android.app.Activity} thành mt hp thoi trên
màn hình ln. Phương pháp mà bn chn ph thuc vào thiết kế ng dng ca bn, nhưng
vic hin th mt hot động thành mt hp thoi thường có ích khi ng dng ca bn đã được thiết kế cho màn hình
nh và bn mun ci thin tri nghim trên máy tính bng bng cách hin th mt hot động có vòng đời ngn
thành mt hp thoi.</p>
<p>Để hin th mt hot động thành mt hp thoi ch khi trên màn hình ln,
hãy áp dng ch đề {@link android.R.style#Theme_Holo_DialogWhenLarge Theme.Holo.DialogWhenLarge}
cho phn t bn kê khai <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
&lt;activity&gt;}</a>:</p>
<pre>
&lt;activity android:theme="&#64;android:style/Theme.Holo.DialogWhenLarge" >
</pre>
<p>Để biết thêm thông tin v vic to kiu cho các hot động ca bn bng ch đề, hãy xem hướng dn <a href="{@docRoot}guide/topics/ui/themes.html">Kiu và Ch đề</a>.</p>
<h2 id="DismissingADialog">B mt Hp thoi</h2>
<p>Khi người dùng chm vào bt k nút hành động nào được to bng
{@link android.app.AlertDialog.Builder}, h thng s b hp thoi cho bn.</p>
<p>H thng cũng b hp thoi khi người dùng chm vào mt mc trong mt danh sách hp thoi, tr
khi danh sách s dng nút chn mt hoc hp kim. Nếu không, bn có th b th công hp thoi ca mình
bng cách gi {@link android.support.v4.app.DialogFragment#dismiss()} trên {@link
android.support.v4.app.DialogFragment} ca bn.</p>
<p>Trong trường hp bn cn thc hin các
hành động nht định khi hp thoi biến mt, bn có th trin khai phương pháp {@link
android.support.v4.app.DialogFragment#onDismiss onDismiss()} trong {@link
android.support.v4.app.DialogFragment} ca mình.</p>
<p>Bn cũng có th <em>hy bỏ</em> mt hp thoi. Đây là mt s kin đặc bit ch báo người dùng
ch ý ri khi hp thoi mà không hoàn thành tác vụ. Điu này xy ra nếu người dùng nhn nút
<em>Quay li</em>, chm vào màn hình ngoài vùng hp thoi,
hoc nếu bn công khai gi {@link android.app.Dialog#cancel()} trên {@link
android.app.Dialog} (chng hn như khi hi đáp li mt nút "Hủy bỏ" trong hp thoi).</p>
<p>Như nêu trong ví d bên trên, bn có th hi đáp li s kin hy b này bng cách trin khai
{@link android.support.v4.app.DialogFragment#onCancel onCancel()} trong lớp {@link
android.support.v4.app.DialogFragment} ca mình.</p>
<p class="note"><strong>Lưu ý:</strong> H thng s gi
{@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} trên mỗi sự kiện mà
gi ra lnh gi li {@link android.support.v4.app.DialogFragment#onCancel onCancel()}. Tuy nhiên,
nếu bn gi {@link android.app.Dialog#dismiss Dialog.dismiss()} hoặc {@link
android.support.v4.app.DialogFragment#dismiss DialogFragment.dismiss()},
h thng s gi {@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} <em>chứ
không phi</em> {@link android.support.v4.app.DialogFragment#onCancel onCancel()}. Vì thế, nhìn chung bạn nên
gi {@link android.support.v4.app.DialogFragment#dismiss dismiss()} khi người dùng nhấn nút
<em>tích cc</em> trong hộp thoại của bạn để xóa hộp thoại khỏi dạng xem.</p>