AlertDialog和ProgressDialog的使用
AlertDialog 確認(rèn)取消對(duì)話框
AlertDialog可以在當(dāng)前頁(yè)面彈出一個(gè)對(duì)話框,這個(gè)對(duì)話框置于所有界面元素之上葬凳,能夠屏蔽掉和其他組件的交互能力
代碼實(shí)現(xiàn):
//拿到的AlertDialog對(duì)話框的創(chuàng)建器對(duì)象
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle("This is Dialog");
dialogBuilder.setMessage("Something important.");
dialogBuilder.setCancelable(false); //設(shè)置為false,則點(diǎn)擊back鍵或者彈窗外區(qū)域既们,彈窗不消去
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ //使用了匿名內(nèi)部類
@override
public void onClick(DialogInterface dialog, int which){
//加入邏輯代碼
//對(duì)話框消失的方法
dialog.dismiss();
}
}
);
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ //使用了匿名內(nèi)部類
@override
public void onClick(DialogInterface dialog, int which){
//加入邏輯代碼
}
}
);
//使用對(duì)話框創(chuàng)建器來(lái)創(chuàng)建一個(gè)對(duì)話框?qū)ο? AlertDialog alertDialog = dialogBuilder.create();
//將對(duì)話框顯示出來(lái)
alertDialog.show();
實(shí)現(xiàn)的效果圖:
AlertDialog彈出取消確認(rèn)框
AlertDialog 單選對(duì)話框
單選對(duì)話框?qū)嶋H上也是一個(gè)對(duì)話框,所以首先也需要拿到對(duì)話框創(chuàng)建器,而后設(shè)置單選框
代碼如下:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("請(qǐng)選擇性別");
//單選框中的內(nèi)容
final String[] items = new String[]{"男", "女"};
/*
* items:指定單選框中的內(nèi)容
* checkedItem:哪一個(gè)選項(xiàng)默認(rèn)被選中历筝,-1代表未有選項(xiàng)被選中
*/
//設(shè)置單選對(duì)話框
alert.setSingleChoiceItems(items, -1, new OnClickListener() {
/*
* which:用戶所選的條目的下標(biāo)
* dialog:觸發(fā)這個(gè)方法的對(duì)話框
*/
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "您選擇的是" + items[which], Toast.LENGTH_SHORT).show();
//對(duì)話框消失
dialog.dismiss();
}
});
//直接調(diào)用對(duì)話框創(chuàng)建器的show方法也可以顯示出來(lái)對(duì)話框
alert.show();
實(shí)現(xiàn)效果如圖:
AlertDialog 多選對(duì)話框
多選框類似于單選框
代碼如下:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("您覺(jué)著帥的人");
final String[] items = new String[]{"小生", "老生", "單生"};
final boolean[] checkedItems = new boolean[]{false, false, false, false};
//設(shè)置多選對(duì)話框
alert.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
/*
* dialog:該對(duì)話框?qū)ο? * which:用戶點(diǎn)擊的條目的下標(biāo)
* isChecked:條目是否被選中
*/
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
//設(shè)置一個(gè)確定按鈕
alert.setPositiveButton("確定", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//將對(duì)話框消失
dialog.dismiss();
}
});
alert.show();
實(shí)現(xiàn)效果如圖:
ProgressDialog
此控件和AlertDialog相似友鼻,可以在界面上彈出一個(gè)對(duì)話框傻昙,屏蔽掉和其他控件的交互能力闺骚,但ProgressDialog會(huì)在對(duì)話框中顯示一個(gè)進(jìn)度條
代碼實(shí)現(xiàn):
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading");
progressDialog.setCancelable(true);
progressDialog.show(); //將進(jìn)度條顯示出來(lái)
實(shí)現(xiàn)的效果圖:
ProgressDialog彈出的進(jìn)度條
當(dāng)數(shù)據(jù)加載完成后必須要調(diào)用ProgressDialog的dismiss()
方法來(lái)關(guān)閉對(duì)話框,否則ProgressDialog將會(huì)一直存在