在android的編程中,對話方塊的用處是非常多的,然而,有時對對話方塊的需求也是不一樣的,那麼現在,就總結對話方塊的種類,以及事件的處理形勢。
1、簡單的對話方塊
[java]
public void showDialog(){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("標題")
.setMessage("訊息")
.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("不好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
public void showDialog(){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("標題")
.setMessage("訊息")
.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("不好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
2、帶編輯框的對話方塊
[java]
public void showDialog(){
EditText et = new EditText(this);// 單獨出來,方便取使用者輸入的資料。
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(et)
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}})
.create();
dialog.show();
}
public void showDialog(){
EditText et = new EditText(this);// 單獨出來,方便取使用者輸入的資料。
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(et)
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}})
.create();
dialog.show();
}
3、含單選鈕的對話方塊
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取資料
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("單選框,請選擇")
.setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() {
// 3 代表預設選中str的index=3的那個單選框
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取資料
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("單選框,請選擇")
.setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() {
// 3 代表預設選中str的index=3的那個單選框
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
4、含多選框的對話方塊
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取資料
boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("多選框,請選擇")
.setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.e("DIYDialogActivity", "showDialog->" +
"you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取資料
boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("多選框,請選擇")
.setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.e("DIYDialogActivity", "showDialog->" +
"you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
5、含列表的對話方塊
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取資料
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("列表框,供選擇")
.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog---------------------you select " + which);
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取資料
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("列表框,供選擇")
.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog---------------------you select " + which);
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
6、自訂的對話方塊
[java]
public void showDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));
// 可指定對話方塊的背景色彩那些
Dialog dialog = new AlertDialog.Builder(this)
.setView(layout)
.create();
// 設定對話方塊的出現位置,藉助於window對象
Window win = dialog.getWindow();
//win.setGravity(Gravity.CENTER);
//win.setGravity(Gravity.BOTTOM);
//win.setGravity(Gravity.TOP);
win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話方塊時,底部表單,不變暗。
WindowManager.LayoutParams lp = win.getAttributes();
lp.x = -200;// x=0,y=0時,顯示位置是螢幕中心。
lp.y = 0 ;
lp.alpha = 0.6f;// 對話方塊的透明度
win.setAttributes(lp);
Button but = (Button) layout.findViewById(R.id.but_positive);
but.requestFocus();
dialog.show();
}
public void showDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));
// 可指定對話方塊的背景色彩那些
Dialog dialog = new AlertDialog.Builder(this)
.setView(layout)
.create();
// 設定對話方塊的出現位置,藉助於window對象
Window win = dialog.getWindow();
//win.setGravity(Gravity.CENTER);
//win.setGravity(Gravity.BOTTOM);
//win.setGravity(Gravity.TOP);
win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話方塊時,底部表單,不變暗。
WindowManager.LayoutParams lp = win.getAttributes();
lp.x = -200;// x=0,y=0時,顯示位置是螢幕中心。
lp.y = 0 ;
lp.alpha = 0.6f;// 對話方塊的透明度
win.setAttributes(lp);
Button but = (Button) layout.findViewById(R.id.but_positive);
but.requestFocus();
dialog.show();
}