반응형

[TIP]

RadioGroup 메소드중에 clearCheck()을 호출하면 RadioGroup 안에 모든 Button의 체크 상태가 해제됨  

 

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

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
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.isNotEmpty
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "애완동물 사진 보기"
        ChkAgree.setOnCheckedChangeListener { compoundButton, b ->
            if(b){
                Text2.visibility = android.view.View.VISIBLE
                RdoDog.visibility = android.view.View.VISIBLE
                RdoCat.visibility = android.view.View.VISIBLE
                RdoRabbit.visibility = android.view.View.VISIBLE
                BtnFinish.visibility = android.view.View.VISIBLE
                BtnReset.visibility = android.view.View.VISIBLE
            }
            else{
                Rgroup1.clearCheck()
                Text2.visibility = android.view.View.INVISIBLE
                RdoDog.visibility = android.view.View.INVISIBLE
                RdoCat.visibility = android.view.View.INVISIBLE
                RdoRabbit.visibility = android.view.View.INVISIBLE
                BtnFinish.visibility = android.view.View.INVISIBLE
                BtnReset.visibility = android.view.View.INVISIBLE
                ImgPet.visibility = android.view.View.INVISIBLE
            }
        }
        Rgroup1.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.RdoDog -> {
                    ImgPet.setImageResource(R.drawable.dog);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
                R.id.RdoCat -> {
                    ImgPet.setImageResource(R.drawable.cat);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
                R.id.RdoRabbit-> {
                    ImgPet.setImageResource(R.drawable.rabbit);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
            }
        }
        BtnFinish.setOnClickListener {
            finish()
        }
        BtnReset.setOnClickListener {
            Rgroup1.clearCheck()
            Text2.visibility = android.view.View.INVISIBLE
            RdoDog.visibility = android.view.View.INVISIBLE
            RdoCat.visibility = android.view.View.INVISIBLE
            RdoRabbit.visibility = android.view.View.INVISIBLE
            BtnFinish.visibility = android.view.View.INVISIBLE
            BtnReset.visibility = android.view.View.INVISIBLE
            ImgPet.visibility = android.view.View.INVISIBLE
            ChkAgree.isChecked = false
        }
    }
}
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
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Text1"
        android:text="선택을 시작하겠습니까?"/>
    <Switch
        android:id="@+id/ChkAgree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="시작함"/>
    <TextView
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Text2"
        android:text="좋아하는 애완동물은?"/>
    <RadioGroup
        android:id="@+id/Rgroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoDog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="강아지"/>
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoCat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="고양이"/>
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoRabbit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="토끼"/>
    </RadioGroup>
 
    <ImageView
        android:id="@+id/ImgPet"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"
        android:adjustViewBounds="true"/>
    <Button
        android:visibility="invisible"
        android:id="@+id/BtnFinish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="종료"/>
    <Button
        android:visibility="invisible"
        android:id="@+id/BtnReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="처음으로"/>
 
 
</LinearLayout>
cs

 

반응형

+ Recent posts