列举一些 Android 常见的 Dailog 样式。
普通对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public void commondialog(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false); builder.setIcon(android.R.drawable.ic_dialog_email); builder.setTitle("这是标题"); builder.setMessage("这是弹窗内容");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", null); builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
} }); AlertDialog dialog = builder.create(); dialog.show();
}
}
|
列表对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public void listdialog(View v){ final String[] items = new String[]{"张三","李四","王五"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_email); builder.setTitle("这是标题");
builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
String item = items[which]; Toast.makeText(getApplicationContext(),item,0).show(); } });
AlertDialog dialog = builder.create(); dialog.show();
}
|
单选对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public void singleChoiceDialog(View v){ final String[] items = new String[]{"张三","李四","王五"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_email); builder.setTitle("这是标题");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String item = items[which]; Toast.makeText(getApplicationContext(),item,0).show(); dialog.dismiss();
} });
AlertDialog dialog = builder.create(); dialog.show();
}
}
|
多选对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public void multiChoiceDialog(View v){ final String[] items = new String[]{"张三","李四","王五"}; final boolean[] checkItems = new boolean[]{true,true,false};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_email); builder.setTitle("这是标题"); builder.setMultiChoiceItems(items, checkItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkItems[which] = isChecked; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { boolean c = checkItems[1]; System.out.println(c); StringBuffer sb = new StringBuffer("您选择的项目是:"); for (int i = 0; i < checkItems.length ; i++){ if (checkItems[i]) { sb.append(items[i]).append(",");
}
} Toast.makeText(getApplicationContext(),sb.toString(),1).show();
} });
AlertDialog dialog = builder.create(); dialog.show();
}
}
|
等候对话框
1 2 3 4 5 6
| public void spinnerDialog(View v) { new ProgressDialog(this).show();
} }
|
进度条对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public void hprogressDialog(View v) { final ProgressDialog pd = new ProgressDialog(this); pd.setProgress(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(300); pd.setIcon(android.R.drawable.ic_dialog_email); pd.setMessage("正在下载"); pd.show();
new Thread(){ public void run(){ int i = 1; while(i < 250){ pd.setProgress(i++); SystemClock.sleep(300); } pd.dismiss(); } }.start();
} }
|