FileDocCategorySizeDatePackage
NumberPicker.javaAPI DocAndroid 1.5 API13300Wed May 06 22:41:56 BST 2009com.android.internal.widget

NumberPicker

public class NumberPicker extends android.widget.LinearLayout implements android.view.View.OnFocusChangeListener, android.view.View.OnLongClickListener, android.view.View.OnClickListener

Fields Summary
public static final Formatter
TWO_DIGIT_FORMATTER
private final android.os.Handler
mHandler
private final Runnable
mRunnable
private final android.widget.EditText
mText
private final android.text.InputFilter
mNumberInputFilter
private String[]
mDisplayedValues
private int
mStart
private int
mEnd
private int
mCurrent
private int
mPrevious
private OnChangedListener
mListener
private Formatter
mFormatter
private long
mSpeed
private boolean
mIncrement
private boolean
mDecrement
private static final char[]
DIGIT_CHARACTERS
private NumberPickerButton
mIncrementButton
private NumberPickerButton
mDecrementButton
Constructors Summary
public NumberPicker(android.content.Context context)

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

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

        super(context, attrs);
        setOrientation(VERTICAL);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.number_picker, this, true);
        mHandler = new Handler();
        InputFilter inputFilter = new NumberPickerInputFilter();
        mNumberInputFilter = new NumberRangeKeyListener();
        mIncrementButton = (NumberPickerButton) findViewById(R.id.increment);
        mIncrementButton.setOnClickListener(this);
        mIncrementButton.setOnLongClickListener(this);
        mIncrementButton.setNumberPicker(this);
        mDecrementButton = (NumberPickerButton) findViewById(R.id.decrement);
        mDecrementButton.setOnClickListener(this);
        mDecrementButton.setOnLongClickListener(this);
        mDecrementButton.setNumberPicker(this);
        
        mText = (EditText) findViewById(R.id.timepicker_input);
        mText.setOnFocusChangeListener(this);
        mText.setFilters(new InputFilter[] {inputFilter});
        mText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

        if (!isEnabled()) {
            setEnabled(false);
        }
    
Methods Summary
public voidcancelDecrement()

        mDecrement = false;
    
public voidcancelIncrement()

        mIncrement = false;
    
private voidchangeCurrent(int current)

        
        // Wrap around the values if we go past the start or end
        if (current > mEnd) {
            current = mStart;
        } else if (current < mStart) {
            current = mEnd;
        }
        mPrevious = mCurrent;
        mCurrent = current;
        notifyChange();
        updateView();
    
private java.lang.StringformatNumber(int value)

        return (mFormatter != null)
                ? mFormatter.toString(value)
                : String.valueOf(value);
    
public intgetCurrent()

return
the current value.

        return mCurrent;
    
private intgetSelectedPos(java.lang.String str)

        if (mDisplayedValues == null) {
            return Integer.parseInt(str);
        } else {
            for (int i = 0; i < mDisplayedValues.length; i++) {
                
                /* Don't force the user to type in jan when ja will do */
                str = str.toLowerCase();
                if (mDisplayedValues[i].toLowerCase().startsWith(str)) {
                    return mStart + i;
                }
            }
            
            /* The user might have typed in a number into the month field i.e.
             * 10 instead of OCT so support that too.
             */
            try {
                return Integer.parseInt(str);
            } catch (NumberFormatException e) {
                
                /* Ignore as if it's not a number we don't care */
            }
        }
        return mStart;
    
private voidnotifyChange()

        if (mListener != null) {
            mListener.onChanged(this, mPrevious, mCurrent);
        }
    
public voidonClick(android.view.View v)

        validateInput(mText);
        if (!mText.hasFocus()) mText.requestFocus();

        // now perform the increment/decrement
        if (R.id.increment == v.getId()) {
            changeCurrent(mCurrent + 1);
        } else if (R.id.decrement == v.getId()) {
            changeCurrent(mCurrent - 1);
        }
    
public voidonFocusChange(android.view.View v, boolean hasFocus)

        
        /* When focus is lost check that the text field
         * has valid values.
         */
        if (!hasFocus) {
            validateInput(v);
        }
    
public booleanonLongClick(android.view.View v)
We start the long click here but rely on the {@link NumberPickerButton} to inform us when the long click has ended.

        
        /* The text view may still have focus so clear it's focus which will
         * trigger the on focus changed and any typed values to be pulled.
         */
        mText.clearFocus();
        
        if (R.id.increment == v.getId()) {
            mIncrement = true;
            mHandler.post(mRunnable);
        } else if (R.id.decrement == v.getId()) {
            mDecrement = true;
            mHandler.post(mRunnable);
        }
        return true;
    
public voidsetCurrent(int current)

        mCurrent = current;
        updateView();
    
public voidsetEnabled(boolean enabled)

        super.setEnabled(enabled);
        mIncrementButton.setEnabled(enabled);
        mDecrementButton.setEnabled(enabled);
        mText.setEnabled(enabled);
    
public voidsetFormatter(com.android.internal.widget.NumberPicker$Formatter formatter)

        mFormatter = formatter;
    
public voidsetOnChangeListener(com.android.internal.widget.NumberPicker$OnChangedListener listener)

        mListener = listener;
    
public voidsetRange(int start, int end)
Set the range of numbers allowed for the number picker. The current value will be automatically set to the start.

param
start the start of the range (inclusive)
param
end the end of the range (inclusive)

        mStart = start;
        mEnd = end;
        mCurrent = start;
        updateView();
    
public voidsetRange(int start, int end, java.lang.String[] displayedValues)
Set the range of numbers allowed for the number picker. The current value will be automatically set to the start. Also provide a mapping for values used to display to the user.

param
start the start of the range (inclusive)
param
end the end of the range (inclusive)
param
displayedValues the values displayed to the user.

        mDisplayedValues = displayedValues;
        mStart = start;
        mEnd = end;
        mCurrent = start;
        updateView();
    
public voidsetSpeed(long speed)
The speed (in milliseconds) at which the numbers will scroll when the the +/- buttons are longpressed. Default is 300ms.

        mSpeed = speed;
    
private voidupdateView()

        
        /* If we don't have displayed values then use the
         * current number else find the correct value in the
         * displayed values for the current number.
         */
        if (mDisplayedValues == null) {
            mText.setText(formatNumber(mCurrent));
        } else {
            mText.setText(mDisplayedValues[mCurrent - mStart]);
        }
        mText.setSelection(mText.getText().length());
    
private voidvalidateCurrentView(java.lang.CharSequence str)

        int val = getSelectedPos(str.toString());
        if ((val >= mStart) && (val <= mEnd)) {
            mPrevious = mCurrent;
            mCurrent = val;
            notifyChange();
        }
        updateView();
    
private voidvalidateInput(android.view.View v)

        String str = String.valueOf(((TextView) v).getText());
        if ("".equals(str)) {

            // Restore to the old value as we don't allow empty values
            updateView();
        } else {

            // Check the new value and ensure it's in range
            validateCurrentView(str);
        }