AlertDialog代碼示例
AlertDialog是一個(gè)帶有確定取消按鈕的系統(tǒng)彈窗,因?yàn)槭窍到y(tǒng)控件,不需要在layout.xml中注冊(cè),直接使用代碼即可.
void showAlert(){
AlertDialog.Builder dialog = new AlertDialog.Builder(SubActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("Something important.");
dialog.setCancelable(false);
dialog.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//點(diǎn)擊確定,窗口會(huì)自動(dòng)關(guān)閉
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//點(diǎn)擊取消,窗口會(huì)自動(dòng)關(guān)閉
}
});
dialog.show();
}
解釋
setPositiveButton(確定)和setNegativeButton(取消)分別設(shè)置點(diǎn)擊之后的動(dòng)作.
最后使用show()方法調(diào)出dialog;
ProgressDialog示例
ProgressDialog和AlertDialog非常類似,它沒有兩個(gè)按鈕,只有一個(gè)loading動(dòng)畫.
void showProgress(){
ProgressDialog progressDialog = new ProgressDialog(SubActivity.this);
progressDialog.setTitle("This Title");
progressDialog.setMessage("Plese wait");
progressDialog.setCancelable(true);
progressDialog.show();
}
setCancelable() 解釋
這兩個(gè)控件都有setCancelable()方法;
當(dāng)傳入值為true時(shí),點(diǎn)擊彈窗之外的空白區(qū)域可以關(guān)閉彈出窗.