IT/Android

Widget - check box

바바옄 2015. 6. 16. 14:21
반응형

art + insert = getter, setter, 생성자 등을 만드는 단축키

MainActivity.java

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.mycheckbox;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CheckBox;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
 
public class MainActivity extends ActionBarActivity {
 
    private CheckBox chkFishing;
    private CheckBox chkSwim;
    private CheckBox chkSkin;
    private CheckBox chkNon;
 
    private List<String> checkedHobby;
 
    public List<String> getCheckedHobby() {
        return checkedHobby;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        checkedHobby = new ArrayList<String>();
 
        // Inflate 된 Widget을 하나씩 초기화 시킨다,.
        setInitiate();
        // Listener를 등록한다.
        setInitiateListener();
    }
 
    private void setInitiate(){
        chkFishing = (CheckBox) findViewById(R.id.chkFishing);
        chkSwim = (CheckBox) findViewById(R.id.chkSwim);
        chkSkin = (CheckBox) findViewById(R.id.chkSkin);
        chkNon = (CheckBox) findViewById(R.id.chkNon);
    }
 
    private void setInitiateListener() {
 
        MyHobbyClickListener myHobbyClickListener = new MyHobbyClickListener(this);
 
        chkFishing.setOnClickListener(myHobbyClickListener);
        chkSwim.setOnClickListener(myHobbyClickListener);
        chkSkin.setOnClickListener(myHobbyClickListener);
        chkNon.setOnClickListener(myHobbyClickListener);
    }
 
    public void setChecked(int checkBoxId, boolean isChecked){
 
        if(checkBoxId == R.id.chkFishing){
            chkFishing.setChecked(isChecked);
            if(isChecked){
                chkNon.setChecked(false);
            }
        }
        else if(checkBoxId == R.id.chkSwim){
            chkSwim.setChecked(isChecked);
            if(isChecked){
                chkNon.setChecked(false);
            }
        }
        else if(checkBoxId == R.id.chkSkin){
            chkSkin.setChecked(isChecked);
            if(isChecked){
                chkNon.setChecked(false);
            }
        }
        else if(checkBoxId == R.id.chkNon){
            chkNon.setChecked(isChecked);
            if(isChecked) {
                chkFishing.setChecked(false);
                chkSwim.setChecked(false);
                chkSkin.setChecked(false);
            }
        }
    }
 
    public void showCheckedHobbies(){
 
        String message = "";
 
        for(String hobby : checkedHobby){
            message += hobby + ",";
        }
        if(message.length()>0){
            // 끝자리 Comma 제거
            message = message.substring(0,message.length()-1);
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}
 
cs

 

MyHobbyClickListener.java

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
package com.ktds.mycheckbox;
 
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
 
/**
 * Created by 206-001 on 2015-06-16.
 */
public class MyHobbyClickListener implements CheckBox.OnClickListener{
 
    // activity 의 super class
    private Context context;
 
    public MyHobbyClickListener(Context context) {
        this.context = context;
    }
 
    @Override
    public void onClick(View v) {
 
        // checkbox 선택 여부
        boolean isChecked = ((CheckBox )v).isChecked();
 
        //선택된 checkbox id 가져옴
        int checkBoxId = v.getId();
 
        if(checkBoxId == R.id.chkFishing){
            if(isChecked) {
                ((MainActivity) context).getCheckedHobby().add("낚시");
            }
            else{
                ((MainActivity) context).getCheckedHobby().remove("낚시");
            }
        }
        else if(checkBoxId == R.id.chkSwim){
            if(isChecked) {
                ((MainActivity) context).getCheckedHobby().add("수영");
            }
            else{
                ((MainActivity) context).getCheckedHobby().remove("수영");
            }
        }
        else if(checkBoxId == R.id.chkSkin){
            if(isChecked) {
                ((MainActivity) context).getCheckedHobby().add("스킨스쿠버");
            }
            else{
                ((MainActivity) context).getCheckedHobby().remove("스킨스쿠버");
            }
        }
        else if(checkBoxId == R.id.chkNon){
            if(isChecked) {
                ((MainActivity) context).getCheckedHobby().clear();
            }
        }
 
        ((MainActivity) context).setChecked(checkBoxId,isChecked);
        ((MainActivity) context).showCheckedHobbies();
    }
}
 
cs

 

activity_main.xml

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
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--
        제약사항 : ScrollView는 단 하나의 View 밖에 처리하지 못한다.
        ScrollView 안에 LinearLayout을 한번 더 감싸서 하나의 view로 묶어 줌
    -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
 
            <TextView
                android:text="취미를 선택하세요"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
 
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="낚시"
                android:id="@+id/chkFishing"
                android:checked="true"
                />
 
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="수영"
                android:id="@+id/chkSwim" />
 
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="스킨스쿠버"
                android:id="@+id/chkSkin" />
 
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="없음"
                android:id="@+id/chkNon" />
 
 
        </LinearLayout>
 
    </ScrollView>
 
 
</LinearLayout>
cs

 

          

           

 

 

 

 

 

 

 

 

반응형

'IT > Android' 카테고리의 다른 글

Widget - ImageView  (0) 2015.06.16
Widget - Switch  (0) 2015.06.16
실습  (0) 2015.06.16
Widget - RadioButton  (0) 2015.06.16
Android 프로그래밍  (0) 2015.06.15