[android : kotlin] 코틀린 setOnCheckedChangeListener() 사용 방법 및 RadioButton사용 예제
코틀린 setOnCheckedChangeListener()
setOnCheckedChangeListener() 리스너는 사용자가 라디오 버튼을 체크하거나 해제했을 때 반응하는 이벤트 처리를 하기 위해 사용된다. setOnCheckedChangeListener()를 사용하는 방법에는 inner class로 생성 후 사용하는 방법과 람다식을 사용하는 방법이 있다. 개발자 마다 좋아하는 코딩 스타일이 있으니 두 가지 방법 중에 아무거나 사용해도 된다. 람다식 방법으로 코딩시 미사용된 group의 경우 언더바( _ )로 처리가 가능하다. 라디오 버튼 중에 하나만 선택 가능하도록 하기위해서 RadioGroup으로 묶어 준다.
[MainActivity.kt]
package edu.kotlin.study
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioGroup
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button1.setOnClickListener {
when (radioGp1.checkedRadioButtonId) {
R.id.radioButton -> text1.text = "라디오 버튼 1선택 됨\n"
R.id.radioButton2 -> text1.text = "라디오 버튼 2선택 됨\n"
R.id.radioButton3 -> text1.text = "라디오 버튼 3선택 됨\n"
}
}
//inner class 리스너 생성 후 초기화 하는 방법
radioGp1.setOnCheckedChangeListener(CheckboxListener())
//람다식 방법
radioGp1.setOnCheckedChangeListener { group, checkedId ->
when(checkedId) {
R.id.radioButton -> text1.text = "라디오 버튼 1번째 체크됨"
R.id.radioButton2 -> text1.text = "라디오 버튼 2번째 체크됨"
R.id.radioButton3 -> text1.text = "라디오 버튼 3번째 체크됨"
}
}
//람다식 방법
radioGp1.setOnCheckedChangeListener { _, checkedId ->
when(checkedId) {
R.id.radioButton -> text1.text = "라디오 버튼 1번째 체크됨"
R.id.radioButton2 -> text1.text = "라디오 버튼 2번째 체크됨"
R.id.radioButton3 -> text1.text = "라디오 버튼 3번째 체크됨"
}
}
}
inner class CheckboxListener : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when (group?.id) {
R.id.radioGp1 ->
when(checkedId) {
R.id.radioButton -> text1.text = "라디오 버튼 1번째 체크됨"
R.id.radioButton2 -> text1.text = "라디오 버튼 2번째 체크됨"
R.id.radioButton3 -> text1.text = "라디오 버튼 3번째 체크됨"
}
}
}
}
}
[activity_main.xml]
<?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">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World!"
android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
<RadioGroup
android:id="@+id/radioGp1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton3" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="상태 확인" />
</LinearLayout>
[build.gradle(:app)]
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId "edu.kotlin.study"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

