欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 系统dialog的应用

程序员文章站 2022-05-21 17:35:01
应用示例如下: ......

应用示例如下:

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