Android AlertDialog ————多选对话框

//弹出一个多选对话框
    private void showMutilDialog() {
        //[1]构造对话框的实例
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("请选择你喜欢吃的水果");
        //[1.1]★★★★★★
        final String items[] = {"西瓜","香蕉","苹果","黄瓜","胡萝卜","柿子"};
        final boolean[] checkedItems = {true,false,false,false,false,true};
        builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            }
        });
        //[2]设置确定和取消按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < checkedItems.length; i++) {
                    //判断一下是选中的
                    if (checkedItems[i]) {
                        //把选中的水果取出来     数据在哪里存着就去哪里取
                        String fruit = items[i];
                        sb.append(fruit + " ");
                    }
                }
                Toast.makeText(getApplicationContext(), sb.toString(), 1).show();
                dialog.dismiss();
            }
        });
        //[3]展示对话框  和toast一样 一定要记得show出来
        builder.show();
    }

 

好人不留名,拿去用。哈哈哈哈哈

你可能感兴趣的:(Android,基础知识)