有的時候,可能需要彈出一個對話方塊,以便從使用者的輸入來擷取某些確認資訊。這種情況下,可以重寫Activity基類中的受保護方法(protected)onCreateDialog()。
1、建立一個工程:Dialog。
2、main.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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.2cto.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的專欄