Android_RadioGroup switch문 사용 Fragment에서

Posted by PeEn
2019. 4. 8. 03:40 Programing/Java

Fragment 상속받아 사용할 때 Radio버튼을 누를 때 어떤 버튼이 눌렸는지 확인해서 원하는 값을 Toast로 출력하기

FragmentHead.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
100
101
package com.example.administrator.serviceproject;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
 
public class FragmentHead extends Fragment {
    static int score;
    View myView;
    public Button button;
    public FragmentHead() {
 
    }
 
    public void toastout(){
        Toast.makeText(getActivity(), score+"점", Toast.LENGTH_LONG).show();
    }
 
 
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        myView = inflater.inflate(R.layout.fragment_head, container, false);
        RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radioGroup);
 
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
 
                Button radioGroup = (Button)getActivity().findViewById(checkedId);
 
                switch (checkedId) {
                    case R.id.headbutton0:
                        score = 0;
                        toastout();
                         break;
                    case R.id.headbutton1:
                        score = 1;
                        toastout();
                        break;
                    case R.id.headbutton2:
                        score = 2;
                        toastout();
                        break;
                    case R.id.headbutton3:
                        score = 3;
                        toastout();
                        break;
                    case R.id.headbutton4:
                        score = 4;
                        toastout();
                        break;
                    case R.id.headbutton5:
                        score = 5;
                        toastout();
                        break;
                    case R.id.headbutton6:
                        score = 6;
                        toastout();
                        break;
                    case R.id.headbutton7:
                        score = 7;
                        toastout();
                        break;
                    case R.id.headbutton8:
                        score = 8;
                        toastout();
                        break;
                    case R.id.headbutton9:
                        score = 9;
                        toastout();
                        break;
                    case R.id.headbutton10:
                        score = 10;
                        toastout();
                        break;
                    case R.id.headbutton11:
                        score = 11;
                        toastout();
                        break;
                    case R.id.headbutton12:
                        score = 12;
                        toastout();
                        break;
                     default:
                         Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                }
            }
        });
 
 
 
return myView;
}}
cs

fragment_head.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentHead">
 
    <LinearLayout
        android:id="@+id/headlinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 
        <TextView
            android:id="@+id/headtitle"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="@string/head"
            android:textAppearance="@style/TextAppearance.AppCompat.Button"
            android:textColor="#000022"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="0dp" />
 
        <ScrollView
            android:id="@+id/headscroll"
            android:layout_width="match_parent"
            android:layout_height="687dp">
 
            <android.support.constraint.ConstraintLayout
 
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
 
 
                <RadioGroup
                    android:id="@+id/radioGroup"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/headtitle">
 
                    <TextView
                        android:id="@+id/headgroup1"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#9BEF92"
                        android:gravity="center"
                        android:text="@string/headgroup1"
                        android:textAppearance="@style/TextAppearance.AppCompat.Button"
                        android:textColor="#FFFFFF"
                        android:textStyle="bold"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton0"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:contentDescription="@string/headonclick"
                        android:text="@string/headbutton0"
                        app:layout_constraintTop_toBottomOf="@+id/headgroup1"
                        tools:layout_editor_absoluteX="16dp" />
 
                    <TextView
                        android:id="@+id/headgroup2"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#9BEF92"
                        android:gravity="center"
                        android:text="@string/headgroup2"
                        android:textAppearance="@style/TextAppearance.AppCompat.Button"
                        android:textColor="#FFFFFF"
                        android:textStyle="bold"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton0"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton1"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton1"
                        app:layout_constraintTop_toBottomOf="@+id/headgroup2"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton2"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton2"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton1"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton3"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton3"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton2"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton4"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton4"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton3"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton5"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton5"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton4"
                        tools:layout_editor_absoluteX="16dp" />
 
                    <TextView
                        android:id="@+id/headgroup4"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#9BEF92"
                        android:gravity="center"
                        android:text="@string/headgroup4"
                        android:textAppearance="@style/TextAppearance.AppCompat.Button"
                        android:textColor="#FFFFFF"
                        android:textStyle="bold"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton5"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton6"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton6"
                        app:layout_constraintTop_toBottomOf="@+id/headgroup4"
                        tools:layout_editor_absoluteX="41dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton7"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton7"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton6"
                        tools:layout_editor_absoluteX="27dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton8"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton8"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton7"
                        tools:layout_editor_absoluteX="16dp" />
 
                    <TextView
                        android:id="@+id/headgroup3"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#9BEF92"
                        android:gravity="center"
                        android:text="@string/headgroup3"
                        android:textAppearance="@style/TextAppearance.AppCompat.Button"
                        android:textColor="#FFFFFF"
                        android:textStyle="bold"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton8"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton9"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
 
                        android:text="@string/headbutton9"
                        app:layout_constraintTop_toBottomOf="@+id/headgroup3"
                        tools:layout_editor_absoluteX="16dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton10"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton10"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton9"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton11"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton11"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton10"
                        tools:layout_editor_absoluteX="0dp" />
 
                    <RadioButton
                        android:id="@+id/headbutton12"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:text="@string/headbutton12"
                        app:layout_constraintTop_toBottomOf="@+id/headbutton11"
                        tools:layout_editor_absoluteX="0dp" />
 
                </RadioGroup>
 
 
            </android.support.constraint.ConstraintLayout>
        </ScrollView>
    </LinearLayout>
</FrameLayout>
cs