Android Application

Kotlin 대화상자(Dialog) : 시간추출 대화상자(TimePickerDialog)

작성자 임베디드코리아 작성일23-09-25 01:31 조회1,996회 댓글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">

    <Button
        android:id="@+id/btn_load_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="시간 선택"/>

    <TextView
        android:id="@+id/text_selected_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_weight="1"/>

</LinearLayout>

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

        var BTN_load_timer: Button = findViewById(R.id.btn_load_timer)
        var TEXT_selected_time: TextView = findViewById(R.id.text_selected_time)

        val cal = Calendar.getInstance()

        // 타이머 로드
        BTN_load_timer.setOnClickListener {
            TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { timePicker, h, m ->
                TEXT_selected_time.text = "$h:$m"
            }, cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), true ).show()
        }

    }
}