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

Android对话框Dialog的使用

程序员文章站 2022-03-09 16:11:37
Dialog的使用方法...

普通对话框

	private void showAlertDialogShow(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        alertDialog.setIcon(R.mipmap.ic_launcher);
        alertDialog.setTitle("确定选择?");
        alertDialog.setMessage("........");
        alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"三连支持",Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"下次一定",Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setNeutralButton("忽视", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"假装看不见",Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.show();
    }

列表对话框

	private void showListDialogShow(){
        AlertDialog.Builder listDialog = new AlertDialog.Builder(MainActivity.this);
        listDialog.setTitle("谁是最强的男人?");
        listDialog.setIcon(R.mipmap.ic_launcher);
        final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
            }
        });
        listDialog.show();
	}

单选对话框

	int choice;
    private void showSingleChoiceDialogShow(){
        final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
        AlertDialog.Builder singleDialog = new AlertDialog.Builder(MainActivity.this);
        singleDialog.setTitle("谁是最强的男人?");
        singleDialog.setIcon(R.mipmap.ic_launcher);
        //参数2为默认选项
        singleDialog.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                choice = which;
            }
        });
        singleDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,items[choice],Toast.LENGTH_SHORT).show();
            }
        });
        singleDialog.show();
    }

多选对话框

	ArrayList<Integer> choices= new ArrayList<>();
    	private void showMultiDialogShow(){
        choices.clear();
        final boolean isChecked[] = {false,false,false};
        final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
        AlertDialog.Builder multiDialog = new AlertDialog.Builder(MainActivity.this);
        multiDialog.setTitle("谁是最强的男人?");
        multiDialog.setIcon(R.mipmap.ic_launcher);
        multiDialog.setMultiChoiceItems(items, isChecked, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    choices.add(which);
                }else{
                    choices.remove(which);
                }
            }
        });
        multiDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String s="";
                for (Integer choice : choices) {
                    s = s+items[choice];
                }
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });
        multiDialog.show();
    }

参考文章

传送门

本文地址:https://blog.csdn.net/weixin_43742354/article/details/103945159

相关标签: Android