ぐーたらIT

シロルのぐーたら休日録

IT技術について勉強メモを書いていくブログです。

Android Studio 目覚まし時計を作ってみる Part1

シロルです。
javaの基本的な文法は理解できたので、目覚まし時計を作ってみたいと思います。
流れとしては
アラームを設定 → アラームが鳴る → 別のアクティビティ起動 → 止める
という感じで作ろうと思います。

とりあえず置きましたみたいな感じですがこれでいきます。
f:id:sironekosukaru:20171223205626p:plain

まずはactivity_main.xmlファイルを見ていきたいと思います。

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="60sp"
        android:timeZone="Asia/Tokyo"
        android:format24Hour="HH:mm:ss"
        />

    <EditText
        android:id="@+id/hour"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="時"/>

    <EditText
        android:id="@+id/minute"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="分"/>

    <TextView
        android:id="@+id/confview"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:textSize="24sp" />

        <Button
            android:id="@+id/enter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="enter" />
    </LinearLayout>

1つずつ解説していきます。

  1. TextClock
<TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="60sp"
        android:timeZone="Asia/Tokyo"
        android:format24Hour="HH:mm:ss"
        />

これは現在時刻を表示してくれます。
android:timeZoneでどこの地域の時刻を表示するかを指定しています。
android:format24Hourは24時間で時刻を表示します。
「HH」は時間を2桁
「mm」は分を2桁
「ss」は秒を2桁

  1. EditText
<EditText
        android:id="@+id/hour"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="時"/>

    <EditText
        android:id="@+id/minute"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="分"/>

これは入力したデータを受け取る入力フォームみたいなやつです。
ここで重要なのは、
android:id="@+id/minute"
minuteがこのEditTextのIDになります。自分がわかりやすいIDをつけましょう(名前を自分でつけて識別できるようにするイメージ)。後でプログラムを書くのに使います。

  1. TextView
<TextView
        android:id="@+id/conf"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:textSize="24sp" />

これは設定した時刻を表示するのに使います。EditText同様分かりやすいIDをつけます。今回はconfです。

  1. Button
<Button
            android:id="@+id/enter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="enter" />

このボタンを押すことによりアラームを設定します。
android:text="enter"はボタンに文字を表示させます。今回はenterとしていますが、なぜ大文字で表示されている…
IDもenterです。

今回はここまで。
次回はjavaを書いていきます。