AlertDialog之常见对话框(单选对话框、多选对话框、进度条对话框)
单选对话框,顾名思义就是只能选一项(setSingleChoiceItems(Items,))
1 public void click(View v){ 2 //创建对话框类 3 AlertDialog.Builder builder = new AlertDialog.Builder(this); 4 //定义item选项 5 final String items[] = new String[]{"一只","两只","三只"}; 6 builder.setTitle("没有什么事是一只口红解决不了的,如果有那就两只")//设置标题 7 .setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {//设置单选 8 @Override 9 public void onClick(DialogInterface dialogInterface, int i) { 10 Toast.makeText(MainActivity.this,"你选择了用"+items[i]+"口红解决问题",Toast.LENGTH_SHORT).show(); 11 } 12 }).setPositiveButton("是的", new DialogInterface.OnClickListener() { 13 @Override 14 public void onClick(DialogInterface dialogInterface, int i) { 15 Toast.makeText(MainActivity.this,"我选择了是",Toast.LENGTH_SHORT).show(); 16 } 17 }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 18 @Override 19 public void onClick(DialogInterface dialogInterface, int i) { 20 Toast.makeText(MainActivity.this,"我选择了取消,因为我相信口红解决不了",Toast.LENGTH_SHORT).show(); 21 } 22 }).show(); 23 }
下面是多选对话框
1 public void click1(View v){ 2 AlertDialog.Builder builder = new AlertDialog.Builder(this); 3 final String items[] = {"First","Second","Thrid","Fourth"}; 4 final boolean boo[] = {true,false,false,false}; 5 6 builder.setTitle("人生有多种选择") 7 .setMultiChoiceItems(items, boo, new DialogInterface.OnMultiChoiceClickListener() { 8 @Override 9 public void onClick(DialogInterface dialogInterface, int i, boolean b) { 10 11 } 12 }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 13 @Override 14 public void onClick(DialogInterface dialogInterface, int i) { 15 16 } 17 }).show(); 18 }
单选是setSingleChoiceItems(),多选是setMultiChoiceItems(),
进度条对话框
1 public void click2(View v){ 2 final ProgressDialog pd = new ProgressDialog(this); 3 pd.setTitle("加载"); 4 pd.setProgress(0); 5 pd.setMax(10); 6 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 7 pd.show(); 8 new Thread(){ 9 @Override 10 public void run() { 11 super.run(); 12 for (int i=0; i<=10; i++){ 13 pd.setProgress(i); 14 try { 15 Thread.sleep(1000); 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 } 20 } 21 }.start(); 22 }