본문으로 바로가기

[Android] 선택지 만들기 / select box

category Back-end/Android 2016. 6. 10. 11:19



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.ktds.jgbaek.mydialog;
 
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    private Button btnDialog;
    private Button btnDialog2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnDialog = (Button) findViewById(R.id.btnDialog);
        btnDialog2 = (Button) findViewById(R.id.btnDialog2);
 
        final List selectedItems = new ArrayList();
 
        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = new String[]{"It/Computer""Game""Fassion""VR""Kidult""Sports""Music""Movie"};
 
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("관심분야를 선택하세요.")
                        .setMultiChoiceItems(
                                items,
                                new boolean[]{falsefalsefalsefalsefalsefalsefalsefalse},
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        if(isChecked){
                                            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
                                            selectedItems.add(items[which]);
                                        }
                                        else {
                                            selectedItems.remove(items[which]);
                                        }
                                    }
                                })
                        .setPositiveButton("확인"new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if(selectedItems.size()==0){
                                    Toast.makeText(MainActivity.this"선택된 관심 분야가 없습니다.", Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    String items = "";
                                    for(String seletedItem : selectedItems){
                                        items += (seletedItem + ", ");
                                    }
                                    selectedItems.clear();
 
                                    items = items.substring(0, items.length()-2);
                                    Toast.makeText(MainActivity.this, items, Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).create().show();
            }
        });
        btnDialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = new String[]{"It/Computer""Game""Fassion""VR""Kidult""Sports""Music""Movie"};
                final int[] selectedIndex = {0};
 
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("관심분야를 선택하세요.")
                        .setSingleChoiceItems(
                                items,
                               0,
                               new DialogInterface.OnClickListener() {
                                   @Override
                                   public void onClick(DialogInterface dialog, int which) {
                                       selectedIndex[0= which;
                                   }
                               }
                        )
                        .setPositiveButton("확인"new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, items[selectedIndex[0]], Toast.LENGTH_SHORT).show();
                            }
                        }).create().show();
            }
        });
    }
}
 
cs




items string list안에 선택지 리스트를 만듭니다.

boolean 리스트는 체크 여부를 결정합니다.

아래코드는 여러개를 선택할 수 있는 select box입니다.

MultiChoice로 진행합니다.




아래는 라디오 박스로 선택할 수 있는 Choice입니다.

select할 인덱스를 final로 지정해줘야하기 때문에 값을 변경시킬 수 없습니다. 그래서 하나의 값만 받아야하지만 배열로 선언해줍니다.




결과화면 :




멀티초이스





싱글초이스