본문 바로가기
Android App/Kotlin

위젯-RadioButton

by AppJinny 2022. 11. 2.

*위젯-RadioButton

-RadioButton : 여러 개의 선택지 중에서 하나만 선택 시 사용

-RadioGroup : 라디오버튼들을 그룹으로 묶어서 사용

 

*라디오그룹 속성

-orientation : 라디오버튼들의 정렬(vertical-세로(기본값), horizental-가로)

-checkedButton : 미리 선택되어 있는 버튼 설정(ex. android:checkedButton="@id/버튼아이디")

*라디오버튼 속성

-checked : 미리 선택되어 있는 버튼 설정(ex. android:checked="true")

 

 

*라디오버튼 사용(RadioGroup으로 그룹화 하여 사용)

-activity_main.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/radioGruop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

<!--  라디오 버튼 생성  -->
        <RadioButton
            android:id="@+id/radioApple"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="사과" />
        <RadioButton
            android:id="@+id/radioBanana"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="바나나" />
        <RadioButton
            android:id="@+id/radioOrange"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="오렌지" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

 

-build.gradle

-- android{} 스코프

//뷰바인딩 추가
buildFeatures{
    viewBinding true
}

 

-MainActivity.kt

class MainActivity : AppCompatActivity() {

    //뷰바인딩 추가
    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

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

        //뷰바인딩 추가
        setContentView(binding.root)

        //라디오그룹 연결
        //첫 번째 파라미터:변경된 라디오그룹 위젯, 두 번째 파라미터:라디오그룹 안에 클릭된 라디오버튼 id
        binding.radioGruop.setOnCheckedChangeListener { radioGroup, i ->
            // 라오버튼 id가 클릭 되었을때 발생하는 이벤트
            when(i){
                //R.id 접두어 : 안드로이드가 리소스를 관리하는 R클래스 생성 후 리소스를 관리하는 id클래스 추가
                R.id.radioApple -> Log.d("RadioButton","사과 선택")
                R.id.radioBanana -> Log.d("RadioButton","바나나 선택")
                R.id.radioOrange -> Log.d("RadioButton","오렌지 선택")
            }
        }
    }
}

 

-결과

 

 


이 포스팅에 작성한 내용은 고돈호, ⌜이것이 안드로이드다⌟, 한빛미디어(주), 2022 에서 발췌하였습니다.

'Android App > Kotlin' 카테고리의 다른 글

위젯-ProgressBar  (0) 2022.11.03
위젯-CheckBox, ToggleButton, Switch  (0) 2022.11.02
위젯-Button, ImageButton  (0) 2022.11.02
위젯-TextView, Edit Text  (0) 2022.11.02
레이아웃(Layout) 사용 예제  (0) 2022.11.02