0
0

More than 3 years have passed since last update.

はじめに

本日はTimePickerDialogについて話していきたいと思います

環境

⚫︎OS : macOS Catalina 10.15.7
⚫︎Androidsutdio : 4.2.1
⚫︎Kotlin : 1.5.21
⚫︎Gradle : 7.0.2

TimePickerDialogとは

TimePickerDialogは、時刻の入力に優れたGUIコンポーネントのこと。

めちゃくちゃ参考にさせて頂きました

実装


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvAddTime"
            android:layout_width="227dp"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:text="@string/tv_add_time"
            android:textSize="17dp" />


        <Button
            android:id="@+id/btAddTime"
            android:layout_width="179dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:onClick="showTimePickerDialog"
            android:text="@string/add_date"
            android:textSize="20dp"/>


    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:layout_marginLeft="8dp"
        android:text="@string/tv_add_memo"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/MemoField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <EditText
            android:id="@+id/etAddMemo"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:inputType="text" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/MemoInsertButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onInsertButtonClick"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="25dp"
        android:text="@string/bt_insert_memo" />
</LinearLayout>

スクリーンショット 2021-08-27 23.43.38.png

この様な画面になります。
本来この画面では空文字チェックや時刻とメモをDBに登録などを行なっている画面ですが、今回はダイアログに焦点を当てていきます。
やることとしてはセットと書かれているボタンをタップすると、ダイアログが表示されるという感じにしていきます
スクリーンショット 2021-08-27 23.48.20.png


    // タイマー設定ボタンをクリックした時のメソッド
    fun showTimePickerDialog(v: View) {
        //TImePickクラス呼び出し
        val newFragment = TimePick()
        newFragment.show(supportFragmentManager, "timePicker")

    }

ボタンが押された際の処理を記述しています。

TimePick.kt

   class TimePick : DialogFragment(), TimePickerDialog.OnTimeSetListener {


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //インスタンス
        val c = Calendar.getInstance()
       //最初に、システムの現在の時間と分を取得
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)

        return TimePickerDialog(
            activity,
            activity as MemoAddActivity?,//返したいアクティビティをセット
            hour,
            minute,
            true)
    }

    override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
        //
    }
}

アクティビティに戻ります


   class MemoAddActivity : FragmentActivity(), TimePickerDialog.OnTimeSetListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_memo_add)
    }
 
    override fun onTimeSet(view: TimePicker , hourOfDay: Int , minute: Int) {

        val str = String.format(Locale.US, "%d:%d", hourOfDay, minute)

        val timeView = findViewById<TextView>(R.id.tvAddTime)
        timeView.text = str
    }

onTimeSet() を持ってきて時刻を取得し、TextViewに格納しています

ここまで記述すると
スクリーンショット 2021-08-28 0.11.27.png
TextViewに取得した時刻が表示されています
スクリーンショット 2021-08-28 0.12.25.png

おしまい

実際のこの画面では空文字チェックやDB登録なども行なっているので後日記載していきたいと思います!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0