<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb1-1"
android:checked="true"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb1-2"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb1-3"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb2-1"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<RadioButton
android:id="@+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb2-2"
android:checked="true"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<RadioButton
android:id="@+id/rb6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rb2-3"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</RadioGroup>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 상태 가져오기기"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 상태 가져오기기2"/>
</LinearLayout>
package com.mobile.radiobuttontest
import android.os.Bundle
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn.setOnClickListener { view ->
when (rg1.checkedRadioButtonId) {
R.id.rb1 -> tv1.text = "Radio 1-1 Checked"
R.id.rb2 -> tv1.text = "Radio 1-2 Checked"
R.id.rb3 -> tv1.text = "Radio 1-3 Checked"
}
when (rg2.checkedRadioButtonId) {
R.id.rb4 -> tv2.text = "Radio 2-1 Checked"
R.id.rb5 -> tv2.text = "Radio 2-2 Checked"
R.id.rb6 -> tv2.text = "Radio 2-3 Checked"
}
}
/*
var listener = RadioListener()
rg1.setOnCheckedChangeListener(listener)
rg2.setOnCheckedChangeListener(listener)
*/
rg1.setOnCheckedChangeListener { radioGroup, i ->
when(i){
R.id.rb1 -> tv1.text = "1-1"
R.id.rb2 -> tv1.text = "1-2"
R.id.rb3 -> tv1.text = "1-3"
}
}
rg2.setOnCheckedChangeListener { radioGroup, i ->
when(i){
R.id.rb4 -> tv2.text = "2-1"
R.id.rb5 -> tv2.text = "2-2"
R.id.rb6 -> tv2.text = "2-3"
}
}
btn2.setOnClickListener { view ->
rb3.isChecked = true
rb6.isChecked = true
}
}
inner class RadioListener : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(p0: RadioGroup?, p1: Int) { // p1 사용자가 선택한 라디오 버튼의 아이디값
when (p0?.id) {
R.id.rg1 ->
when (p1) {
R.id.rb1 -> tv1.text = "1-1"
R.id.rb2 -> tv1.text = "1-2"
R.id.rb3 -> tv1.text = "1-3"
}
R.id.rg2 ->
when (p1) {
R.id.rb4 -> tv2.text = "2-1"
R.id.rb5 -> tv2.text = "2-2"
R.id.rb6 -> tv2.text = "2-3"
}
}
}
}
}
RadioButton은 그룹을 지어서 사용한다. 그룹 내에서 하나의 라디오만 체크될 수 있고 복수 체크를 하고 싶다면 체크박스를 사용하면 된다.
inner class로 리스너를 생성하여 사용하는 법도 있고 아예 람다식안에다가 when을 이용해서 분기를 나눌 수도 있다.
선택의 자유!
궁금하신 점은 덧글로 적어주세요.
'안드로이드' 카테고리의 다른 글
[Android Kotlin] 안드로이드 코틀린 Seek Bar (0) | 2020.03.21 |
---|---|
[Android Kotlin] 안드로이드 코틀린 Progress Bar (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Check Box (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Button (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 TextView (0) | 2020.03.21 |