[Android Studio] RadioGroup & CheckBox

han91·2024년 4월 1일

[AndroidStudio]

목록 보기
1/15

RadioGroup과 CheckBox의 비교

class MainActivity : AppCompatActivity() {

    val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.rdGroup.setOnCheckedChangeListener { radioGroup, i ->
            when(i) {
                R.id.apple -> {
                    Log.d("Checked", "Apple Button checked")
                    binding.imgBtn.visibility = View.VISIBLE
                }
                R.id.banana -> {
                    Log.d("Checked", "Banana Button checked")
                    binding.imgBtn.visibility = View.INVISIBLE
                }
                R.id.orange -> Log.d("Checked", "Orange Button checked")
            }
        }
    }
}

조금 변화를 주고 싶어서 apple 버튼을 클릭하면 지정된 ImageButton이 보여지게 만들었음
반대로 banana 버튼을 클릭하면 ImageButton이 보이지 않게 됨
초기 설정은 보이지 않게 xml에서 설정했음

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <RadioGroup
        android:id="@+id/rdGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/apple"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Apple" />

        <RadioButton
            android:id="@+id/banana"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Banana" />

        <RadioButton
            android:id="@+id/orange"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Orange" />
    </RadioGroup>

    <ImageButton
        android:id="@+id/imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@+id/rdGroup"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/cookie" />
</androidx.constraintlayout.widget.ConstraintLayout>

RadioGroup 안에 RadioButton들을 넣어서 만듦

아래는 CheckBox의 Activity 코드

class MainActivity : AppCompatActivity() {

    val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    val listener by lazy {
        CompoundButton.OnCheckedChangeListener { compoundButton, isChecked ->
            if (isChecked) {
                when(compoundButton.id) {
                    R.id.Apple -> Log.d("CheckBox", "Apple is checked")
                    R.id.Banana -> Log.d("CheckBox", "Banana is checked")
                    R.id.Orange -> Log.d("CheckBox", "Orange is checked")
                }
            }
            else {
                when(compoundButton.id) {
                    R.id.Apple -> Log.d("CheckBox", "Apple is unchecked")
                    R.id.Banana -> Log.d("CheckBox", "Banana is unchecked")
                    R.id.Orange -> Log.d("CheckBox", "Orange is unchecked")
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        
        binding.Apple.setOnCheckedChangeListener(listener)
        binding.Banana.setOnCheckedChangeListener(listener)
        binding.Orange.setOnCheckedChangeListener(listener)

    }
}

위 코드는 listener를 따로 설정해둔 케이스
listener를 따로 설정하지 않아도 되긴 하지만 그러면 코드를 작성할 때 번거로워짐
binding을 사용한 이유도 마찬가지

CheckBox의 박스를 클릭하면 log창으로 해당 CheckBox가 선택되었다는 메세지가 출력
다시 한 번 클릭하여 체크를 없애면 체크가 사라졌다는 메세지를 log로 출력하게 만듦

그리고 xml코드

<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <CheckBox
            android:id="@+id/Apple"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Apple" />

        <CheckBox
            android:id="@+id/Banana"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Banana" />

        <CheckBox
            android:id="@+id/Orange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Orange" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

LinearLayout 안에 CheckBox를 넣는 방식을 사용
LinearLayout을 사용했기 때문에 각각의 CheckBox의 크기 비율을 정할 수 있었음

profile
천방지축어리둥절빙글빙글돌아가는개발자

0개의 댓글