在之前的學習過程中我們已經(jīng)知道,Android應(yīng)用中最常用的屏幕顯示開發(fā)是基于Activity的,但是,在很多情況下我們需要顯示一個對話框或浮動窗體來完成一些簡單的任務(wù),比如需要讓用戶輸入一些內(nèi)容,或讓用戶確認一些信息。
在Android中,我們可以通過兩種方式來創(chuàng)建對話框:
1. 借助Dialog類,或它的子類(如AlertDialog)
2. 使用Activity的對話框主題
使用Dialog類:
讓我們先來看下如何借助Dialog類創(chuàng)建對話框,首先,我們需要定義一個繼承了Dialog類的子類:
class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context);
}
}
然后,為這個對話框的內(nèi)容定義一個布局文件,比如:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
android:id=”@+id/widget28″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android”>
<TextView
android:id=”@+id/nameMessage”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Enter Name:”></TextView>
<EditText
android:id=”@+id/nameEditText”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textSize=”18sp”></EditText>
<LinearLayout
android:id=”@+id/buttonLayout”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center_horizontal”>
<Button
android:id=”@+id/okButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”OK”></Button>
<Button
android:id=”@+id/cancelButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Cancel”></Button>
</LinearLayout>
</LinearLayout>
接著,將上面這份布局文件應(yīng)用到我們的對話框上來:
class MyDialog extends Dialog {
….
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(”TestApp”, “Dialog created”);
setContentView(R.layout.mydialog);
}
}
現(xiàn)在,我們就可以調(diào)用這個對話框類的show方法來讓它顯示:
…
MyDialog dialog = new MyDialog(context);
dialog.show();
…
對話框組件的事件處理機制和Activity是相同的,讓我們來看下如何處理對話框中的OK和Cancle按鈕的事件:
class MyDialog extends Dialog implements OnClickListener {
private Button okButton;
private Button cancelButton;
private EditText nameEditText;
protected void onCreate(Bundle savedInstanceState) {
okButton = (Button) findViewById(R.id.okButton);
cancelButton = (Button) findViewById(R.id.cancelButton);
nameEditText = (EditText) findViewById(R.id.nameEditText);
okButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.okButton:
dismiss();
break;
case R.id.cancelButton:
cancel();
break;
}
}
}
在對話框關(guān)閉時,對話框類的dismiss()方法將會被調(diào)用,該方法可以被對話框自身調(diào)用,也可以被其他外部代碼調(diào)用。
對話框支持“取消”功能,“取消”的含義是指不再需要執(zhí)行對話框上的任何功能和動作。對話框的取消可以通過調(diào)用cancel()方法來實現(xiàn)。取消對話框也將會自動調(diào)用dismiss()方法。
當用戶點擊手機設(shè)備上的“返回”按鈕時,屏幕上的對話框?qū)蝗∠绻阆胱屇愕膶υ捒虿辉谶@種情況下被取消掉的話,你可以如下設(shè)置你的對話框:
setCancelable(false);
對話框的取消和關(guān)閉事件可以通過OnCancelListener和OnDismissListener兩個監(jiān)聽器來被監(jiān)聽處理。
從對話框中返回信息:
現(xiàn)在,到了我們要從對話框上獲取用戶輸入的值,將它返回到主調(diào)的Activity中的時候了。然而, Dialog類并沒有提供可以直接返回這些值的方法…但是,我們可以使用自己創(chuàng)建的監(jiān)聽類:
public interface MyDialogListener {
public void onOkClick(String name);
public void onCancelClick();
}
我們的對話框類的構(gòu)造方法同時需要作一點小小的修改:
public MyDialog(Context context, MyDialogListener listener) {
super(context);
this.listener = listener;
}
接著,你就得在創(chuàng)建這個對話框的時候提供一個已經(jīng)實現(xiàn)了MyDialogListener這接口的監(jiān)聽器實現(xiàn)對象了。
然后,我們要在對話框的onclick方法中傳出這個值:
public void onClick(View view) {
switch (view.getId()) {
case R.id.okButton:
listener.onOkClick(nameEditText.getText().toString());
dismiss();
break;
case R.id.cancelButton:
cancel();
break;
}
}
使用AlertDialog:
AlertDialog類是Dialog類的子類。它默認提供了3個按鈕和一個文本消息。這些按鈕可以按需要來使他們顯示或隱藏。下列代碼將創(chuàng)建一個AlertDialog對話框,對話框上將向用戶展示一個問題以及備選的yes/no答案:
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setMessage(”Do you play cricket?”);
dialog.setButton(”Yes”, myOnClickListener);
dialog.setButton2(”No”, myOnClickListener);
dialog.show();
myOnClickListener這個事件監(jiān)聽器的代碼可以類似如下:
public void onClick(DialogInterface dialog, int i) {
switch (i) {
case AlertDialog.BUTTON1:
break;
case AlertDialog.BUTTON2:
break;
}
}
AlertDialog.Builder:
AlertDialog類中有一個內(nèi)部類,名為‘Builder’,Builder類提供了為對話框添加多選或單選列表,以及為這些列表添加事件處理的功能。另外,這個Builder類將AlertDialog對話框上的3個按鈕按照他們的位置分別稱呼為:PositiveButton, NeutralButton, NegativeButton
下列代碼是一個多選列表的例子:
new AlertDialog.Builder(context)
.setIcon(R.drawable.icon)
.setTitle(R.string.alert_dialog_multi_choice)
.setMultiChoiceItems(
R.array.select_dialog_items,
new boolean[]{false, true, false, true, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
}
}).setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}
).create();
Activity托管對話框:
Android也提供了創(chuàng)建對話框的快捷方式,在Activity中可以通過如showDialog(), onCreateDialog(), onPrepareDialog(),dismissDialog(), removeDialog()等方法來創(chuàng)建和管理對話框。
Activity的onCreateDialog方法用于在創(chuàng)建并顯示對話框的時候調(diào)用,比如:
@Override
protected Dialog onCreateDialog(int id) {
return new AlertDialog.Builder(this).setMessage(”How are you?”).setPositiveButton(
”Fine”,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton(”Not so good”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}
).create();
}
你可以同時創(chuàng)建多個對話框,通過為他們設(shè)置id參數(shù)來區(qū)分它們,然后可以通過 showDialog(id)方法來顯示。 onCreateDialog方法只會在第一次調(diào)用showDialog方法時才會被調(diào)用,在之后的showDialog()的調(diào)用中,對話框不是被新建出來的,而是直接顯示之前創(chuàng)建過的那些對話框。
如果你想要更新對話框的內(nèi)容,你只要在 onPrepareDialog()中作相應(yīng)的工作就可以了,該方法會在對話框顯示之前進行調(diào)用。
dismissDialog()方法是用來關(guān)閉對話框的;removeDialog()方法用來將對話框從Activity的托管中移除(如果對已經(jīng)移除的對話框重新進行調(diào)用showDialog ,則該對話框?qū)⑦M行重新創(chuàng)建)。
使用Dialog主題:
另外一種簡單的顯示對話框的方式是讓Activity以Dialog的方式來工作(假裝?),這種Activity被稱作浮動Activity。這種Activity可以通過配置它的主題來實現(xiàn),我們可以在AndroidManifest.xml中進行類似如下的配置:
<activity android:name=”.DialogActivity” android:label=”@string/activity_dialog” android:theme=”@android:style/Theme.Dialog”>
…
</activity>
這樣,該Activity就會應(yīng)用了 ‘Theme.Dialog’的主題而看起來像對話框了。