Android Application
Kotlin 고급 위젯 - 뷰 컨테이너(View Container) : 라디오 그룹(RadioGroup)
작성자 임베디드코리아
작성일23-09-24 02:44
조회1,607회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?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/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GROUP1"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GROUP2"
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/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 상태 가져오기기1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 상태 가져오기기2"/>
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var btn1: Button
private lateinit var btn2: Button
private lateinit var rg1: RadioGroup
private lateinit var rg2: RadioGroup
private lateinit var rb3: RadioButton
private lateinit var rb6: RadioButton
private lateinit var tv1: TextView
private lateinit var tv2: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1 = findViewById(R.id.btn1)
btn2 = findViewById(R.id.btn2)
rg1 = findViewById(R.id.rg1)
rg2 = findViewById(R.id.rg2)
rb3 = findViewById(R.id.rb3)
rb6 = findViewById(R.id.rb6)
tv1= findViewById(R.id.tv1)
tv2= findViewById(R.id.tv2)
btn1.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"
}
}
}
}
}