Time Picker | Android Developers
http://developer.android.com/intl/ja/resources/tutorials/views/hello-timepicker.html
DatePickerと同じタイプの、時刻を選択するウィジェットです。HelloTimePickerというプロジェクトを作成します。
res/values/strings.xmlを開き、以下のように変更します。
1 2 3 4 5 6 7 8 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, HelloTimePickerActivity!</ string > < string name = "app_name" >HelloTimePicker</ string > < string name = "pick_time" >Change the time</ string > </ resources > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "vertical" > < TextView android:id = "@+id/timeDisplay" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "" /> < Button android:id = "@+id/pickTime" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/pick_time" /> </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package com.blogspot.logroid.helloTimePicker; import java.util.Calendar; import android.app.Activity; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class HelloTimePickerActivity extends Activity { private TextView mTimeDisplay; private Button mPickTime; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0 ; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); // capture our View elements mTimeDisplay = (TextView) findViewById(R.id.timeDisplay); mPickTime = (Button) findViewById(R.id.pickTime); // add a click listener to the button mPickTime.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); // get the current time final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); // display the current date updateDisplay(); } // updates the time we display in the TextView private void updateDisplay() { mTimeDisplay.setText( new StringBuilder() .append(pad(mHour)).append( ":" ) .append(pad(mMinute))); } private static String pad( int c) { if (c >= 10 ) return String.valueOf(c); else return "0" + String.valueOf(c); } // the callback received when the user "sets" the time in the dialog private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; updateDisplay(); } }; @Override protected Dialog onCreateDialog( int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog( this , mTimeSetListener, mHour, mMinute, false ); } return null ; } } |

ソースはDate Pickerの時と殆ど同じなので、重複箇所は割愛します。
- TimePickerDialog
時刻選択ダイアログ
- OnTimeSetListener
TimePickerのダイアログの値変更時に呼び出されるイベントリスナ―
- onTimeSet
「設定」ボタンがタップされた時に呼ばれるメソッド
0 件のコメント:
コメントを投稿