티스토리 뷰

반응형


체크 리스트 다이얼로그 생성 및 갯수 제한하기.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
 
            AlertDialog.Builder build = new AlertDialog.Builder(this);
            build.create();
            build.setTitle("숫자 선택");
            build.setIcon(R.drawable.ic_launcher);
            
            final String[] items = {"하나","둘","셋","넷","다섯","여섯","일곱"};
            boolean[] checkedItems = { falsefalsefalsefalsefalsefalsefalse };
            
            build.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) 
                {
                    checkedItems[which] = isChecked;
                    int cnt = 0;
                    for (int i = 0; i <checkedItems.length; i++) {
                        if (checkedItems[i]) {
                            cnt++;
                        }
                    }
                    
                    if (cnt > 3) {
                        Toast.makeText(RegistInfoActivity.this"최대 3개까지 선택 가능합니다.", Toast.LENGTH_SHORT).show();
                        checkedItems[which] = false;
                        ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                    }
                }
            });
 
            OnClickListener listener = new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
 
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        String str = "";
                        int selectCount = 0;
                        for (int i = 0; i <items.length; i++) {
                            if (checkedItems[i]) {
                                selectCount++;
                                if(selectCount <= 3) {
                                    str += items[i] + ", ";
                                }
                            }
                        }
                        str = str.substring(0, str.length()-2);
 
                        Toast.makeText(RegistInfoActivity.this"선택한 건: " + str,
                                Toast.LENGTH_SHORT).show();
                    }else if( which == DialogInterface.BUTTON_NEUTRAL) {
                        dialog.dismiss();
                    }
                }
            };
            
            build.setPositiveButton("선택", listener);
            build.setNeutralButton("취소", listener);
            build.setCancelable(true);
            build.create().show();
 
 
 
cs


반응형
댓글
반응형