android 系统dialog的应用

时光总嘲笑我的痴心妄想 提交于 2020-03-07 06:12:15

应用示例如下:

/*
* 提示类型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();
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!