Android Application

Kotlin 고급 위젯 - 뷰 컨테이너(View Container) : 라디오버튼(RadioButton)

작성자 임베디드코리아 작성일23-09-24 02:39 조회1,560회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      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">

    <TextView
        android:layout_width="400dp"
        android:layout_height="50dp"
        android:text="필요한 색을 선택하세요."
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/radioGroup"
        tools:layout_editor_absoluteX="149dp" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <RadioButton
            android:id="@+id/radioColor1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/red"/>

        <RadioButton
            android:id="@+id/radioColor2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/blue"/>
        <RadioButton
            android:id="@+id/radioColor3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/green"/>

    </RadioGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        setOnCheckedChangeListener()

    }

    private fun setOnCheckedChangeListener() {
        binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->

            when (checkedId) {
                R.id.radioColor1-> {
                  Toast.makeText(applicationContext, "RED 버튼이 선택되었습니다.", Toast.LENGTH_SHORT).show()
                }

                R.id.radioColor2-> {
                    Toast.makeText(applicationContext, "BLUE 버튼이 선택되었습니다.", Toast.LENGTH_SHORT).show()
                }

                R.id.radioColor3-> {
                    Toast.makeText(applicationContext, "GREEN 버튼이 선택되었습니다.", Toast.LENGTH_SHORT).show()
                }
            }
            //Toast.makeText(this, "RED 버튼이 선택되었습니다.", Toast.LENGTH_SHORT).show()

          // Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
        }
    }

}

------------------------------------------------------------------------------------
    strings.xml.kt
-------------------------------------------------------------------------------------
<resources>
    <string name="app_name">RadioButtonBindingKotlin</string>
    <string name="red">빨강</string>
    <string name="blue">파랑</string>
    <string name="green">초록</string>
</resources>