有的時候,可能需要彈出一個對話框,以便從用戶的輸入來獲取某些確認信息。這種情況下,可以重寫Activity基類中的受保護方法(protected)onCreateDialog()。
1、創建一個工程:Dialog。
2、main.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to display a dialog"
android:onClick="onClick" />
</LinearLayout>
3、DialogActivity.java中的代碼。
package net.horsttnann.Dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class DialogActivity extends Activity {
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean[items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View v) {
showDialog(0);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text…")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked!"
: " unchecked!"),
Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
4、調試。
點擊按鈕彈出對話框,在CheckBox上面打勾,就會彈出一個Toast提示,顯示選中物件的文本信息。點擊“OK”或“Cancel”按鈕會使對話框消失。
想要顯示對話框,首先要重寫Activity基類中的onCreateDialog()方法:
@Override www.aiwalls.com
protected Dialog onCreateDialog(int id) {
// …
}
當調用showDialog()的時候,上面被重寫的方法就被調用瞭:public void onClick(View v) {
showDialog(0);
}
這個創建對話框的onCreateDialog()方法是一個被Activity控制的回調函數,當調用showDialog()時,onCreateDialog()回調函數就被觸發瞭。showDialog()方法接受一個Integer參數,用來識別到底要顯示哪個對話框。一般情況下,使用switch語句去判斷顯示不同的對話框。
想要創建一個對話框,還需要使用AlertDialog類的Builder構造器,設置不同的屬性,比如圖標、標題、按鈕、單選框等等:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("This is a dialog with some simple text…");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked!"
: " unchecked!"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
摘自 manoel的專欄