1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.widget.TextView; 23 import android.widget.TimePicker; 24 import android.os.Bundle; 25 26 public class DateWidgets2 extends Activity { 27 28 // where we display the selected date and time 29 private TextView mTimeDisplay; 30 31 32 @Override onCreate(Bundle savedInstanceState)33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 36 setContentView(R.layout.date_widgets_example_2); 37 38 TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker); 39 timePicker.setCurrentHour(12); 40 timePicker.setCurrentMinute(15); 41 42 mTimeDisplay = (TextView) findViewById(R.id.dateDisplay); 43 44 updateDisplay(12, 15); 45 46 timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { 47 48 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 49 updateDisplay(hourOfDay, minute); 50 } 51 }); 52 } 53 updateDisplay(int hourOfDay, int minute)54 private void updateDisplay(int hourOfDay, int minute) { 55 mTimeDisplay.setText( 56 new StringBuilder() 57 .append(pad(hourOfDay)).append(":") 58 .append(pad(minute))); 59 } 60 pad(int c)61 private static String pad(int c) { 62 if (c >= 10) 63 return String.valueOf(c); 64 else 65 return "0" + String.valueOf(c); 66 } 67 68 } 69