Android Application

Kotlin 대화상자(Dialog) : 라디오버튼

작성자 임베디드코리아 작성일23-10-30 21:51 조회1,493회 댓글0건

첨부파일

Kotlin 대화상자(Dialog) : 라디오버튼
-------------------------------------------------------------------------------------
      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">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="112dp"
        android:background="@drawable/button"
        android:text="대화상자"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.057" />
</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    botton.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : button  , Root Element : shape
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#8EC7F4" />
    <stroke android:color="#ff0000ff" android:width="4dp" />
    <padding android:left="7dp" android:top="7dp"
        android:right="7dp" android:bottom="7dp" />
    <corners
        android:radius="30dp"
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
</shape>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button1 = findViewById<View>(R.id.button1) as Button
        button1.setOnClickListener {
            var versionArray = arrayOf("오레오", "파이", "큐(10)")
            var dlg = AlertDialog.Builder(this@MainActivity)
            dlg.setTitle("좋아하는 버전은?")
            dlg.setIcon(R.mipmap.ic_launcher)
            dlg.setSingleChoiceItems(versionArray, 0) { dialog, which ->
                button1.text = versionArray[which]
            }
            dlg.setPositiveButton("닫기", null)
            dlg.show()
        }
    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.RadioButtonDialog" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#9C27B0</item>
    </style>

    <style name="Theme.RadioButtonDialog" parent="Base.Theme.RadioButtonDialog" />
</resources>