Android-timepicker-control

提供:Dev Guides
移動先:案内検索

Android-タイムピッカー

'_Android Time Pickerでは、24時間モードまたはAM/PMモードで時刻を選択できます。 時間は、時間、分、およびクロック形式で構成されます。 Androidは、TimePickerクラスを通じてこの機能を提供します。_

TimePickerクラスを使用するには、最初にactivity.xmlでTimePickerコンポーネントを定義する必要があります。 以下のように定義されています-

<TimePicker
   android:id="@+id/timePicker1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

時間を設定

その後、TimePickerクラスのオブジェクトを作成し、上記で定義されたxmlコンポーネントの参照を取得する必要があります。 その構文は次のとおりです。

import android.widget.TimePicker;
private TimePicker timePicker1;
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

ユーザーが画面上で選択した時間を取得するには、TimePickerクラスのgetCurrentHour()およびgetCurrentMinute()メソッドを使用します。 以下に構文を示します。

int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();

これらのメソッドとは別に、APIにはTimePickerコンポーネントをさらに制御できる他のメソッドがあります。 それらは以下にリストされています。

Sr.No Method & description
1

is24HourView()

このメソッドは、24時間表示の場合はtrueを返し、そうでない場合はfalseを返します。

2

isEnabled()

このメソッドは、このビューの有効状態を返します

3

setCurrentHour(Integer currentHour)

このメソッドは現在の時間を設定します

4

setCurrentMinute(Integer currentMinute)

このメソッドは現在の分を設定します

5

setEnabled(boolean enabled)

このメソッドは、このビューの有効状態を設定します

6

setIs24HourView(Boolean is24HourView)

この方法は、24時間モードかAM/PMモードかを設定します

7

setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)

このメソッドは、ユーザーが時刻を調整したことを示すコールバックを設定します

TimePickerクラスの使用方法を示す例を次に示します。 TimePicker Widgetを使用して時間を設定できる基本的なTime Pickerアプリケーションを作成します

この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。

Steps Description
1 You will use Android studio to create an Android application and name it as TimePicker under a package com.example.timepicker.
2 Modify src/MainActivity.java file to add necessary code.
3 Modify the res/layout/activity_main to add respective XML components
4 Modify the res/values/string.xml to add necessary string components
5 Run the application and choose a running android device and install the application on it and verify the results

以下は、変更されたメインアクティビティファイル src/com.example.timepicker/MainActivity.java の内容です。

package com.example.timepicker;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
   private TimePicker timePicker1;
   private TextView time;
   private Calendar calendar;
   private String format = "";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
      time = (TextView) findViewById(R.id.textView1);
      calendar = Calendar.getInstance();

      int hour = calendar.get(Calendar.HOUR_OF_DAY);
      int min = calendar.get(Calendar.MINUTE);
      showTime(hour, min);
   }

   public void setTime(View view) {
      int hour = timePicker1.getCurrentHour();
      int min = timePicker1.getCurrentMinute();
      showTime(hour, min);
   }

   public void showTime(int hour, int min) {
      if (hour == 0) {
         hour += 12;
         format = "AM";
      } else if (hour == 12) {
         format = "PM";
      } else if (hour > 12) {
         hour -= 12;
         format = "PM";
      } else {
         format = "AM";
      }

      time.setText(new StringBuilder().append(hour).append(" : ").append(min)
      .append(" ").append(format));
   }


}

以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:text="@string/time_pick"
      android:textAppearance="?android:attr/textAppearanceMedium"/>

   <Button
      android:id="@+id/set_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="180dp"
      android:onClick="setTime"
      android:text="@string/time_save"/>

   <TimePicker
      android:id="@+id/timePicker1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/set_button"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="24dp"/>

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/timePicker1"
      android:layout_alignTop="@+id/set_button"
      android:layout_marginTop="67dp"
      android:text="@string/time_current"
      android:textAppearance="?android:attr/textAppearanceMedium"/>

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/textView3"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp"
      android:text="@string/time_selected"
      android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

以下は res/values/string.xml の内容です。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TimePicker</string>
   <string name="action_settings">Settings</string>
   <string name="time_picker_example">Time Picker Example</string>
   <string name="time_pick">Pick the time and press save button</string>
   <string name="time_save">Save</string>
   <string name="time_selected"></string>
   <string name="time_current">The Time is:</string>
</resources>

変更したばかりのTimePickerアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Android StudioはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-

Android Time Picker Tutorial