AlertDialog之常见对话框(单选对话框、多选对话框、进度条对话框)

时间:2021-07-09 13:14:58

单选对话框,顾名思义就是只能选一项(setSingleChoiceItems(Items,))

 public void click(View v){
         //创建对话框类
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         //定义item选项
         final String items[] = new String[]{"一只","两只","三只"};
         builder.setTitle("没有什么事是一只口红解决不了的,如果有那就两只")//设置标题
                 .setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {//设置单选
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) {
                         Toast.makeText(MainActivity.this,"你选择了用"+items[i]+"口红解决问题",Toast.LENGTH_SHORT).show();
                 }
                 }).setPositiveButton("是的", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 Toast.makeText(MainActivity.this,"我选择了是",Toast.LENGTH_SHORT).show();
             }
         }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 Toast.makeText(MainActivity.this,"我选择了取消,因为我相信口红解决不了",Toast.LENGTH_SHORT).show();
             }
         }).show();
     }

下面是多选对话框

 public void click1(View v){
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         final String items[] = {"First","Second","Thrid","Fourth"};
         final boolean boo[] = {true,false,false,false};

         builder.setTitle("人生有多种选择")
                 .setMultiChoiceItems(items, boo, new DialogInterface.OnMultiChoiceClickListener() {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i, boolean b) {

                     }
                 }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {

             }
         }).show();
     }

单选是setSingleChoiceItems(),多选是setMultiChoiceItems(),

进度条对话框

 public void click2(View v){
         final ProgressDialog pd = new ProgressDialog(this);
         pd.setTitle("加载");
         pd.setProgress(0);
         pd.setMax(10);
         pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         pd.show();
         new Thread(){
             @Override
             public void run() {
                 super.run();
                 for (int i=0; i<=10; i++){
                     pd.setProgress(i);
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
             }
         }.start();
     }