(一)比较正常的AlertDialog的创建和使用,Alert(警告)
代码设计:
public void bt2(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示标题").
setIcon(R.drawable.ic_launcher).
setMessage("文本的提示信息:你妈喊你回家吃饭了!").
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择就确定哦", 0).show();
}
}).
setNegativeButton("取消", null).
setNeutralButton("退出", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
程序运行后显示的结果: 这里如果点击取消,对话框消失,什么都没有做 如果点击退出,会直接退出程序 如果点击确定,会弹出一个Toast,提示你
(二)设计一个选择菜单样式的对话框
代码设计:
String[] array = new String[] { "音乐", "体育", "舞蹈", "看书" };
public void bt3(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择你最喜欢的课程").
setItems(array, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择" + array[which],
Toast.LENGTH_SHORT).show();
}
}).
create().show();
}
程序运行后显示的结果:
(三)设置一个单选按钮菜单的对话框
其实这个对话框和上面的菜单对话框差不多,只是样式改变了,还有一点不同的是点击一个选项后对话框是不会消失的,所以必须要设置按钮,来使对话框消失 代码设计:
String[] array = new String[] { "音乐", "体育", "舞蹈", "看书" };
public void bt4(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择你最喜欢的课程").
setSingleChoiceItems(array, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择" + array[which],
Toast.LENGTH_SHORT).show();
}
}).
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择确定", Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", null).
create().show();
}
运行后的结果:
(四)多选项的对话框设计
多选框的显示和单选的显示也是有点相似的,都是需要设置按钮来使对话框消失 代码如下:
String[] array = new String[] { "音乐", "体育", "舞蹈", "看书" };
public void bt5(View v) {
boolean[] checkedItems = { true, false, false, true };
final List<String> list = new ArrayList<String>();
list.add("音乐");
list.add("看书");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择你最喜欢的课程").
setMultiChoiceItems(array, checkedItems,
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
list.add(array[which]);
} else {
list.remove(array[which]);
}
}
}).
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中了" + list, 0)
.show();
}
}).
setNegativeButton("取消", null).
create().show();
}
(五)设计一个有输入框的对话框
public void bt7(View v) {
final EditText et = new EditText(this);
new AlertDialog.Builder(this).
setTitle("有输入框的对话框").
setView(et).
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String in = et.getText().toString();
Toast.makeText(MainActivity.this, "输入;" + in, 0).show();
}
}).setNegativeButton("取消", null).
create().show();
}
(六)条形对话框
public void bt2(View v) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setTitle("文件下载");
dialog.setMessage("正在下载。。。");
dialog.setCancelable(false);
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int index = 0;
while (index < dialog.getMax()) {
index++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.setProgress(index);
}
dialog.cancel();
}
}).start();
}
|