Android之对话框Dialog

首先是确认对话框

//确认对话框
    private void showLog1() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("清华大学"); //设置标题
        dialog.setIcon(R.mipmap.ic_launcher);//设置图片
        dialog.setMessage("确认报考清华大学?"); //设置内容
        dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {//确认安妮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你真棒", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { //取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "真遗憾", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();//千万不要忘记将对话框show出来
    }

 单选对话框

 //单选对话框
    private void showLog2() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("选择性别");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = sex[which];
                Toast.makeText(MainActivity.this, "你选择了"+str, Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }

 多选对话框

 //多选对话框
    private void showLog3() {
        AlertDialog.Builder  dialog = new AlertDialog.Builder(this);
        dialog.setTitle("爱好");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setMultiChoiceItems(favorite, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, "我喜欢上了"+favorite[which]+"!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "我不喜欢"+favorite[which]+"!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
       dialog.show();

    }

 列表对话框

 //列表对话框
    private void showLog4() {
        AlertDialog.Builder  dialog = new AlertDialog.Builder(this);
        dialog.setTitle("爱好列表");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setItems(item, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "我喜欢"+item[which]+"!", Toast.LENGTH_SHORT).show();
            }
        });

        dialog.show();
    }

 

posted @ 2016-07-31 18:23  非我非非我  阅读(301)  评论(0编辑  收藏  举报