AlertDialog单选、多选对话框详解

一、AlertDialog不能自己创建,都是通过Builder的create()方法才能创建对话框,而且对话框的控制层以及视图层都是在Builder内部实现的
所以创建对话框,先创建 Builder对象
  
      AlertDialog.Builder builder= new AlertDialog.Builder(this);
二、setMessage,setIcon,setTitle,无论这是哪种对话框 ,这个是通用的;
三、以下是单选对话框,多选对话框,如何显示数据,和保存数据
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
设置确定取消按钮;
    public void click(View view){
        //AlertDialog 设置标题,这几个属性的内容都是通用的
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        //设置内容
        builder.setMessage("确定取消吗?");
        //设置标题
        builder.setTitle("八嘎呀路");
        //设置图标
        builder.setIcon(android.R.drawable.btn_star);
        //设置
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"取消成功",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消失败", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog=builder.create();
        alertDialog.show();
    }
设置单选对话框
    public void click2(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("单选对话框");
设置单选对话框显示的内容
        final String[] str={"男","女"};
        /*Set a list of items to be displayed in the dialog as the content, you will be notified
        of the selected item via the supplied listener. The list will have a check mark displayed
        to the right of the text for the checked item. Clicking on an item in the list will not
        dismiss the dialog. Clicking on a button will dismiss the dialog.
    Parameters
       items
       the items to be displayed.
       checkedItem       //如果是0,则默认是第一个,如果是1 ,则默认是第二个
         specifies which item is checked. If -1 no items are checked.
      listener
   notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked.
  It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
  所以在监听器的内部添加   dialog.dismiss();
              */
        builder.setSingleChoiceItems(str, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
                         //客户点击后,对话框记得消失
                        dialog.dismiss();
                    }
                }
        );
           对话框展现,如果没这条语句,弹不出对话框的
        builder.show();
    }
    public void click3(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("你喜欢什么动物?");
      //该显示几个数据的内容
        final String string[]=new String[]{"老鼠","猫","鸭子","鹅","田鸡"};
     //默认情况下,哪些内容会被勾选
        final boolean b[]=new boolean[]{false,false,false,false,false};
        builder.setMultiChoiceItems(string, b, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            //which 为用户点击的下标
            //isChecked用户是否被勾选中
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
           //    将客户是否被勾选的记录保存到集合中
                b[which] = isChecked;  //保存客户选择的属性是否被勾选
            }
        });
        //设置一个确定和取消按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          //保存数据
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String item="";
              //取出被勾选中的内容
           for(int i=0;i<5;i++){
                 if(b[i]){             //如果被勾线则保存数据
                     item+=string[i]+" ,";
                 }
              }
                Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();
                //对话框消失
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }
}

你可能感兴趣的:(Android视频学习)