无聊了一个下午,对android的对话框进行了简单的总结。
常用的android对话框:标准对话框,单选对话框,复选对话框,加载对话框。
标准对话框:
private void standardDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("这是标准的对话框");
builder.setMessage("确定要退出软件吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "确定按钮:" + which, Toast.LENGTH_LONG)
.show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "取消按钮:" + which, Toast.LENGTH_LONG)
.show();
}
});
builder.create().show();
}
单选对话框:
private void singleDialog() {
final String[] items = { "语文", "数学", "英语" };
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("这是单选对话框");
builder.setSingleChoiceItems(items,1,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, items[which], Toast.LENGTH_LONG)
.show();
}
});
// builder.setItems(items, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(mContext, items[which], Toast.LENGTH_LONG)
// .show();
// }
// });
builder.create().show();
}
复选对话框:
private void multiDialog() {
final String[] items = { "语文", "数学", "英语" };
final boolean[] checkedItems = new boolean[items.length];
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("这是多选对话框");
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
String checkedStr = "";
for (boolean b : checkedItems) {
checkedStr = checkedStr + "," + b;
}
Toast.makeText(mContext, checkedStr, Toast.LENGTH_LONG)
.show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String checkedStr = "";
// 方法一:获取选中的item
/* AlertDialog alertDialog = (AlertDialog) dialog;
ListView listView = alertDialog.getListView();
//SparseBooleanArray.keyAt(i) :int类型,对应着items数组中的下标
//SparseBooleanArray.valueAt(i):boolean类型,对应着checkedItems数组中的值
SparseBooleanArray checkedArr = listView
.getCheckedItemPositions();
int checkedSize = checkedArr.size(); //items的个数
for (int i = 0; i < checkedSize; i++) {
if (checkedArr.valueAt(i)) {
Object item = listView.getAdapter().getItem(
checkedArr.keyAt(i));
checkedStr = checkedStr + "," + item;
}
}*/
// 方法二:获取选中的item
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
checkedStr = checkedStr + "," + items[i];
}
}
Toast.makeText(mContext, checkedStr, Toast.LENGTH_LONG).show();
}
});
builder.create().show();
}
加载对话框:
ProgressDialog mProgressDialog = null;
private void loadingDialog() {
// 方法一:
// mProgressDialog = ProgressDialog.show(mContext, "加载对话框","正在加载数据");
//方法二:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setTitle("加载对话框");
mProgressDialog.setMessage("正在加载数据");
mProgressDialog.show();
new Handler().postDelayed( new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, "加载完毕", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}, 5000);
}
private void hLoadingDialog() {
new AsyncTask<Void, Integer, Void>() {
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setTitle("加载对话框");
mProgressDialog.setMessage("正在加载数据");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(100);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
for(int i = 1 ; i <= 100 ; i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
Toast.makeText(mContext, "加载完毕", Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
}
}.execute();
}
点击下载源码
下一篇《android自定义对话框》