android 系统dialog的应用

应用示例如下:

/*
* 提示类型dialog
*/
private void dialog1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("test");
builder.setMessage("Is it exit?"); //设置内容
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
//which:点击ok,which是-1
//点击cancel,which是-3
//点击ignore,which是-2
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("ignore", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//显示dialog
builder.create().show();
} private void dialog1(){
DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case Dialog.BUTTON_POSITIVE:
break;
case Dialog.BUTTON_NEGATIVE:
break;
case Dialog.BUTTON_NEUTRAL:
break;
}
}
}
};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("test");
builder.setMessage("Is it exit?");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("ok",dialogOnclicListener);
builder.setNegativeButton("cancel", dialogOnclicListener);
builder.setNeutralButton("ignore", dialogOnclicListener);
builder.create().show();
} /*
* 列表类型dialog
*/
private void dialog2() {
final String items[]={"test1","test2","test3"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("test");
builder.setIcon(R.mipmap.ic_launcher);
//设置列表显示,注意设置了列表显示就不要设置builder.setMessage()了,否则列表不起作用。
builder.setItems(items,new DialogInterface.OnClickListener() {
//which是items数组的下标
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
} /*
* 单选类型dialog
*/
private void dialog3(){
final String items[]={"single","double"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("test");
//0表示默认选中是第一个
builder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
} /*
* 多选类型dialog
*/
private void dialog4(){
final String items[]={"North Korea","American","Syria"};
final boolean selected[]={true,false,true};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("test");
//selected是默认的对应的选中状态。当你选择时,系统会自动帮你把selected中的值做相应改变,所以在确定按钮中可以得到所有的选择状态
builder.setMultiChoiceItems(items,selected,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//android会自动根据你选择的改变selected数组的值。
}
});
builder.create().show();
}
上一篇:Notes of the scrum meeting(11/3)


下一篇:JavaWeb之javaBean(顺便在idea中连接下数据库)