Android Application

Kotlin 대화상자(Dialog) : 리스트 대화상자

작성자 임베디드코리아 작성일23-10-30 21:00 조회1,384회 댓글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:text="대화상자"
        android:background="@drawable/button" />
</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)

        var 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.setItems(versionArray) { 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.ListDialog" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#03A9F4</item>
        <item name="colorPrimaryDark">#FF5722</item>
    </style>

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