728x90
하나의 그룹 안에서 하나만 선택할 수 있도록 하는 View
Radio Button 주요속성
text : RadioButton에 표시되는 문자열을 설정한다.
checked : 체크 상태를 설정한다. RadioButton은 그룹 내에서 반드시 하나는 선택되어 있는 상태로 제공되는 목적으로 사용하는 View 이므로 반드시 하나는 체크해야 한다.
Radio Button 주요 프로퍼티
isChecked : RadioButton 체크 상태 값. 체크 상태 설정 시 같은 그룹 내의 RadioButton은 모두 체크가 해제된 상태가 된다.
Radio Group 주요 프로퍼티
checkedRadioButtonId : 그룹 내에서 선택되어 있는 RadioButton의 id
Radio Group 주요 이벤트
checkedChange : 그룹 내의 RadioButton의 체크 상태가 변경되었을 때
package com.example.radiobutton
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)
button.setOnClickListener {
radioButton3.isChecked=true
radioButton6.isChecked=true
}
button2.setOnClickListener {
when (radioGroup1.checkedRadioButtonId) {
R.id.radioButton ->{
textView.text="라디오 1-1이 체크되어 있습니다"
}
R.id.radioButton2->{
textView.text="라디오 1-2이 체크되어 있습니다"
}
R.id.radioButton3 ->{
textView.text="라디오 1-3이 체크되어 있습니다"
}
}
when (radioGroup2.checkedRadioButtonId) {
R.id.radioButton4 ->{
textView2.text="라디오 2-1이 체크되어 있습니다"
}
R.id.radioButton5->{
textView2.text="라디오 2-2이 체크되어 있습니다"
}
R.id.radioButton6 ->{
textView2.text="라디오 2-3이 체크되어 있습니다"
}
}
}
radioGroup1.setOnCheckedChangeListener(listener1)
radioGroup2.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.radioButton4 ->{
textView2.text="라디오 2-1이 체크 되었습니다"
}
R.id.radioButton5 ->{
textView2.text="라디오 2-2이 체크 되었습니다"
}
R.id.radioButton6 ->{
textView2.text="라디오 2-3이 체크 되었습니다"
}
}
}
}
val listener1 = object : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when (group?.id) {
R.id.radioGroup1 -> {
when(checkedId){
R.id.radioButton->{
textView.text="라디오 1-1이 체크되었습니다"
}
R.id.radioButton2->{
textView.text="라디오 1-2이 체크되었습니다"
}
R.id.radioButton3->{
textView.text="라디오 1-3이 체크되었습니다"
}
}
}
}
}
}
}
728x90
'안드로이드 > Widget' 카테고리의 다른 글
안드로이드 CheckedTextView (0) | 2022.05.31 |
---|---|
안드로이드 Switch (0) | 2022.05.30 |
안드로이드 CheckBox (0) | 2022.05.30 |
안드로이드 ToggleButton (0) | 2022.05.30 |
안드로이드 ImageView (0) | 2022.05.30 |