FileDocCategorySizeDatePackage
TimePicker.javaAPI DocAndroid 1.5 API11292Wed May 06 22:41:56 BST 2009android.widget

TimePicker

public class TimePicker extends FrameLayout
A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second (e.g. '1' then '2' to select 12). The minutes can be entered by entering single digits. Under AM/PM mode, the user can hit 'a', 'A", 'p' or 'P' to pick. For a dialog using this view, see {@link android.app.TimePickerDialog}.

Fields Summary
private static final OnTimeChangedListener
NO_OP_CHANGE_LISTENER
A no-op callback used in the constructor to avoid null checks later in the code.
private int
mCurrentHour
private int
mCurrentMinute
private Boolean
mIs24HourView
private boolean
mIsAm
private final com.android.internal.widget.NumberPicker
mHourPicker
private final com.android.internal.widget.NumberPicker
mMinutePicker
private final Button
mAmPmButton
private final String
mAmText
private final String
mPmText
private OnTimeChangedListener
mOnTimeChangedListener
Constructors Summary
public TimePicker(android.content.Context context)


                    
       

                                   
              
    

       
        this(context, null);
    
public TimePicker(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
public TimePicker(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.time_picker,
            this, // we are the parent
            true);

        // hour
        mHourPicker = (NumberPicker) findViewById(R.id.hour);
        mHourPicker.setOnChangeListener(new NumberPicker.OnChangedListener() {
            public void onChanged(NumberPicker spinner, int oldVal, int newVal) {
                mCurrentHour = newVal;
                if (!mIs24HourView) {
                    // adjust from [1-12] to [0-11] internally, with the times
                    // written "12:xx" being the start of the half-day
                    if (mCurrentHour == 12) {
                        mCurrentHour = 0;
                    }
                    if (!mIsAm) {
                        // PM means 12 hours later than nominal
                        mCurrentHour += 12;
                    }
                }
                onTimeChanged();
            }
        });

        // digits of minute
        mMinutePicker = (NumberPicker) findViewById(R.id.minute);
        mMinutePicker.setRange(0, 59);
        mMinutePicker.setSpeed(100);
        mMinutePicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        mMinutePicker.setOnChangeListener(new NumberPicker.OnChangedListener() {
            public void onChanged(NumberPicker spinner, int oldVal, int newVal) {
                mCurrentMinute = newVal;
                onTimeChanged();
            }
        });

        // am/pm
        mAmPmButton = (Button) findViewById(R.id.amPm);

        // now that the hour/minute picker objects have been initialized, set
        // the hour range properly based on the 12/24 hour display mode.
        configurePickerRanges();

        // initialize to current time
        Calendar cal = Calendar.getInstance();
        setOnTimeChangedListener(NO_OP_CHANGE_LISTENER);
        
        // by default we're not in 24 hour mode
        setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
        setCurrentMinute(cal.get(Calendar.MINUTE));
        
        mIsAm = (mCurrentHour < 12);
        
        /* Get the localized am/pm strings and use them in the spinner */
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] dfsAmPm = dfs.getAmPmStrings();
        mAmText = dfsAmPm[Calendar.AM];
        mPmText = dfsAmPm[Calendar.PM];
        mAmPmButton.setText(mIsAm ? mAmText : mPmText);
        mAmPmButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                requestFocus();
                if (mIsAm) {
                    
                    // Currently AM switching to PM
                    if (mCurrentHour < 12) {
                        mCurrentHour += 12;
                    }                
                } else {
                    
                    // Currently PM switching to AM
                    if (mCurrentHour >= 12) {
                        mCurrentHour -= 12;
                    }
                }
                mIsAm = !mIsAm;
                mAmPmButton.setText(mIsAm ? mAmText : mPmText);
                onTimeChanged();
            }
        });
        
        if (!isEnabled()) {
            setEnabled(false);
        }
    
Methods Summary
private voidconfigurePickerRanges()

        if (mIs24HourView) {
            mHourPicker.setRange(0, 23);
            mHourPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
            mAmPmButton.setVisibility(View.GONE);
        } else {
            mHourPicker.setRange(1, 12);
            mHourPicker.setFormatter(null);
            mAmPmButton.setVisibility(View.VISIBLE);
        }
    
public intgetBaseline()

        return mHourPicker.getBaseline(); 
    
public java.lang.IntegergetCurrentHour()

return
The current hour (0-23).

        return mCurrentHour;
    
public java.lang.IntegergetCurrentMinute()

return
The current minute.

        return mCurrentMinute;
    
public booleanis24HourView()

return
true if this is in 24 hour view else false.

        return mIs24HourView;
    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        setCurrentHour(ss.getHour());
        setCurrentMinute(ss.getMinute());
    
protected android.os.ParcelableonSaveInstanceState()

    

    
       
        Parcelable superState = super.onSaveInstanceState();
        return new SavedState(superState, mCurrentHour, mCurrentMinute);
    
private voidonTimeChanged()

        mOnTimeChangedListener.onTimeChanged(this, getCurrentHour(), getCurrentMinute());
    
public voidsetCurrentHour(java.lang.Integer currentHour)
Set the current hour.

        this.mCurrentHour = currentHour;
        updateHourDisplay();
    
public voidsetCurrentMinute(java.lang.Integer currentMinute)
Set the current minute (0-59).

        this.mCurrentMinute = currentMinute;
        updateMinuteDisplay();
    
public voidsetEnabled(boolean enabled)

        super.setEnabled(enabled);
        mMinutePicker.setEnabled(enabled);
        mHourPicker.setEnabled(enabled);
        mAmPmButton.setEnabled(enabled);
    
public voidsetIs24HourView(java.lang.Boolean is24HourView)
Set whether in 24 hour or AM/PM mode.

param
is24HourView True = 24 hour mode. False = AM/PM.

        if (mIs24HourView != is24HourView) {
            mIs24HourView = is24HourView;
            configurePickerRanges();
            updateHourDisplay();
        }
    
public voidsetOnTimeChangedListener(android.widget.TimePicker$OnTimeChangedListener onTimeChangedListener)
Set the callback that indicates the time has been adjusted by the user.

param
onTimeChangedListener the callback, should not be null.

        mOnTimeChangedListener = onTimeChangedListener;
    
private voidupdateHourDisplay()
Set the state of the spinners appropriate to the current hour.

        int currentHour = mCurrentHour;
        if (!mIs24HourView) {
            // convert [0,23] ordinal to wall clock display
            if (currentHour > 12) currentHour -= 12;
            else if (currentHour == 0) currentHour = 12;
        }
        mHourPicker.setCurrent(currentHour);
        mIsAm = mCurrentHour < 12;
        mAmPmButton.setText(mIsAm ? mAmText : mPmText);
        onTimeChanged();
    
private voidupdateMinuteDisplay()
Set the state of the spinners appropriate to the current minute.

        mMinutePicker.setCurrent(mCurrentMinute);
        mOnTimeChangedListener.onTimeChanged(this, getCurrentHour(), getCurrentMinute());