在之前的學習過程中我們已經知道,Android應用中最常用的屏幕顯示開發是基于Activity的,但是,在很多情況下我們需要顯示一個對話框或浮動窗體來完成一些簡單的任務,比如需要讓用戶輸入一些內容,或讓用戶確認一些信息。
在Android中,我們可以通過兩種方式來創建對話框:
1. 借助Dialog類,或它的子類(如AlertDialog)
2. 使用Activity的對話框主題
使用Dialog類:
讓我們先來看下如何借助Dialog類創建對話框,首先,我們需要定義一個繼承了Dialog類的子類:
class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context);
}
}
然后,為這個對話框的內容定義一個布局文件,比如:
<?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>
接著,將上面這份布局文件應用到我們的對話框上來:
class MyDialog extends Dialog {
….
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(”TestApp”, “Dialog created”);
setContentView(R.layout.mydialog);
}
}
現在,我們就可以調用這個對話框類的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;
}
}
}
在對話框關閉時,對話框類的dismiss()方法將會被調用,該方法可以被對話框自身調用,也可以被其他外部代碼調用。
對話框支持“取消”功能,“取消”的含義是指不再需要執行對話框上的任何功能和動作。對話框的取消可以通過調用cancel()方法來實現。取消對話框也將會自動調用dismiss()方法。
當用戶點擊手機設備上的“返回”按鈕時,屏幕上的對話框將會被取消,如果你想讓你的對話框不在這種情況下被取消掉的話,你可以如下設置你的對話框:
setCancelable(false);
對話框的取消和關閉事件可以通過OnCancelListener和OnDismissListener兩個監聽器來被監聽處理。
從對話框中返回信息:
現在,到了我們要從對話框上獲取用戶輸入的值,將它返回到主調的Activity中的時候了。然而, Dialog類并沒有提供可以直接返回這些值的方法…但是,我們可以使用自己創建的監聽類:
public interface MyDialogListener {
public void onOkClick(String name);
public void onCancelClick();
}
我們的對話框類的構造方法同時需要作一點小小的修改:
public MyDialog(Context context, MyDialogListener listener) {
super(context);
this.listener = listener;
}
接著,你就得在創建這個對話框的時候提供一個已經實現了MyDialogListener這接口的監聽器實現對象了。
然后,我們要在對話框的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個按鈕和一個文本消息。這些按鈕可以按需要來使他們顯示或隱藏。下列代碼將創建一個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這個事件監聽器的代碼可以類似如下:
public void onClick(DialogInterface dialog, int i) {
switch (i) {
case AlertDialog.BUTTON1:
break;
case AlertDialog.BUTTON2:
break;
}
}
AlertDialog.Builder:
AlertDialog類中有一個內部類,名為‘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也提供了創建對話框的快捷方式,在Activity中可以通過如showDialog(), onCreateDialog(), onPrepareDialog(),dismissDialog(), removeDialog()等方法來創建和管理對話框。
Activity的onCreateDialog方法用于在創建并顯示對話框的時候調用,比如:
@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();
}
你可以同時創建多個對話框,通過為他們設置id參數來區分它們,然后可以通過 showDialog(id)方法來顯示。 onCreateDialog方法只會在第一次調用showDialog方法時才會被調用,在之后的showDialog()的調用中,對話框不是被新建出來的,而是直接顯示之前創建過的那些對話框。
如果你想要更新對話框的內容,你只要在 onPrepareDialog()中作相應的工作就可以了,該方法會在對話框顯示之前進行調用。
dismissDialog()方法是用來關閉對話框的;removeDialog()方法用來將對話框從Activity的托管中移除(如果對已經移除的對話框重新進行調用showDialog ,則該對話框將進行重新創建)。
使用Dialog主題:
另外一種簡單的顯示對話框的方式是讓Activity以Dialog的方式來工作(假裝?),這種Activity被稱作浮動Activity。這種Activity可以通過配置它的主題來實現,我們可以在AndroidManifest.xml中進行類似如下的配置:
<activity android:name=”.DialogActivity” android:label=”@string/activity_dialog” android:theme=”@android:style/Theme.Dialog”>
…
</activity>
這樣,該Activity就會應用了 ‘Theme.Dialog’的主題而看起來像對話框了。