am 1048ad55: Merge change 24395 into eclair
Merge commit '1048ad554ac64224c864f52266b5839cd91d4ee5' into eclair-plus-aosp * commit '1048ad554ac64224c864f52266b5839cd91d4ee5': Make NumberPicker subclassable
This commit is contained in:
@@ -36,7 +36,7 @@ import com.android.internal.R;
|
|||||||
|
|
||||||
public class NumberPicker extends LinearLayout implements OnClickListener,
|
public class NumberPicker extends LinearLayout implements OnClickListener,
|
||||||
OnFocusChangeListener, OnLongClickListener {
|
OnFocusChangeListener, OnLongClickListener {
|
||||||
|
|
||||||
public interface OnChangedListener {
|
public interface OnChangedListener {
|
||||||
void onChanged(NumberPicker picker, int oldVal, int newVal);
|
void onChanged(NumberPicker picker, int oldVal, int newVal);
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
* most efficient way to do this; it avoids creating temporary objects
|
* most efficient way to do this; it avoids creating temporary objects
|
||||||
* on every call to format().
|
* on every call to format().
|
||||||
*/
|
*/
|
||||||
public static final NumberPicker.Formatter TWO_DIGIT_FORMATTER =
|
public static final NumberPicker.Formatter TWO_DIGIT_FORMATTER =
|
||||||
new NumberPicker.Formatter() {
|
new NumberPicker.Formatter() {
|
||||||
final StringBuilder mBuilder = new StringBuilder();
|
final StringBuilder mBuilder = new StringBuilder();
|
||||||
final java.util.Formatter mFmt = new java.util.Formatter(mBuilder);
|
final java.util.Formatter mFmt = new java.util.Formatter(mBuilder);
|
||||||
@@ -63,7 +63,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
return mFmt.toString();
|
return mFmt.toString();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final Handler mHandler;
|
private final Handler mHandler;
|
||||||
private final Runnable mRunnable = new Runnable() {
|
private final Runnable mRunnable = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -81,21 +81,21 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
private final InputFilter mNumberInputFilter;
|
private final InputFilter mNumberInputFilter;
|
||||||
|
|
||||||
private String[] mDisplayedValues;
|
private String[] mDisplayedValues;
|
||||||
private int mStart;
|
protected int mStart;
|
||||||
private int mEnd;
|
protected int mEnd;
|
||||||
private int mCurrent;
|
protected int mCurrent;
|
||||||
private int mPrevious;
|
protected int mPrevious;
|
||||||
private OnChangedListener mListener;
|
private OnChangedListener mListener;
|
||||||
private Formatter mFormatter;
|
private Formatter mFormatter;
|
||||||
private long mSpeed = 300;
|
private long mSpeed = 300;
|
||||||
|
|
||||||
private boolean mIncrement;
|
private boolean mIncrement;
|
||||||
private boolean mDecrement;
|
private boolean mDecrement;
|
||||||
|
|
||||||
public NumberPicker(Context context) {
|
public NumberPicker(Context context) {
|
||||||
this(context, null);
|
this(context, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NumberPicker(Context context, AttributeSet attrs) {
|
public NumberPicker(Context context, AttributeSet attrs) {
|
||||||
this(context, attrs, 0);
|
this(context, attrs, 0);
|
||||||
}
|
}
|
||||||
@@ -117,7 +117,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
mDecrementButton.setOnClickListener(this);
|
mDecrementButton.setOnClickListener(this);
|
||||||
mDecrementButton.setOnLongClickListener(this);
|
mDecrementButton.setOnLongClickListener(this);
|
||||||
mDecrementButton.setNumberPicker(this);
|
mDecrementButton.setNumberPicker(this);
|
||||||
|
|
||||||
mText = (EditText) findViewById(R.id.timepicker_input);
|
mText = (EditText) findViewById(R.id.timepicker_input);
|
||||||
mText.setOnFocusChangeListener(this);
|
mText.setOnFocusChangeListener(this);
|
||||||
mText.setFilters(new InputFilter[] {inputFilter});
|
mText.setFilters(new InputFilter[] {inputFilter});
|
||||||
@@ -127,7 +127,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
setEnabled(false);
|
setEnabled(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
super.setEnabled(enabled);
|
super.setEnabled(enabled);
|
||||||
@@ -135,19 +135,19 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
mDecrementButton.setEnabled(enabled);
|
mDecrementButton.setEnabled(enabled);
|
||||||
mText.setEnabled(enabled);
|
mText.setEnabled(enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOnChangeListener(OnChangedListener listener) {
|
public void setOnChangeListener(OnChangedListener listener) {
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFormatter(Formatter formatter) {
|
public void setFormatter(Formatter formatter) {
|
||||||
mFormatter = formatter;
|
mFormatter = formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the range of numbers allowed for the number picker. The current
|
* Set the range of numbers allowed for the number picker. The current
|
||||||
* value will be automatically set to the start.
|
* value will be automatically set to the start.
|
||||||
*
|
*
|
||||||
* @param start the start of the range (inclusive)
|
* @param start the start of the range (inclusive)
|
||||||
* @param end the end of the range (inclusive)
|
* @param end the end of the range (inclusive)
|
||||||
*/
|
*/
|
||||||
@@ -157,12 +157,12 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
mCurrent = start;
|
mCurrent = start;
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the range of numbers allowed for the number picker. The current
|
* Set the range of numbers allowed for the number picker. The current
|
||||||
* value will be automatically set to the start. Also provide a mapping
|
* value will be automatically set to the start. Also provide a mapping
|
||||||
* for values used to display to the user.
|
* for values used to display to the user.
|
||||||
*
|
*
|
||||||
* @param start the start of the range (inclusive)
|
* @param start the start of the range (inclusive)
|
||||||
* @param end the end of the range (inclusive)
|
* @param end the end of the range (inclusive)
|
||||||
* @param displayedValues the values displayed to the user.
|
* @param displayedValues the values displayed to the user.
|
||||||
@@ -174,7 +174,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
mCurrent = start;
|
mCurrent = start;
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCurrent(int current) {
|
public void setCurrent(int current) {
|
||||||
mCurrent = current;
|
mCurrent = current;
|
||||||
updateView();
|
updateView();
|
||||||
@@ -187,7 +187,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
public void setSpeed(long speed) {
|
public void setSpeed(long speed) {
|
||||||
mSpeed = speed;
|
mSpeed = speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
validateInput(mText);
|
validateInput(mText);
|
||||||
if (!mText.hasFocus()) mText.requestFocus();
|
if (!mText.hasFocus()) mText.requestFocus();
|
||||||
@@ -199,15 +199,15 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
changeCurrent(mCurrent - 1);
|
changeCurrent(mCurrent - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String formatNumber(int value) {
|
private String formatNumber(int value) {
|
||||||
return (mFormatter != null)
|
return (mFormatter != null)
|
||||||
? mFormatter.toString(value)
|
? mFormatter.toString(value)
|
||||||
: String.valueOf(value);
|
: String.valueOf(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void changeCurrent(int current) {
|
protected void changeCurrent(int current) {
|
||||||
|
|
||||||
// Wrap around the values if we go past the start or end
|
// Wrap around the values if we go past the start or end
|
||||||
if (current > mEnd) {
|
if (current > mEnd) {
|
||||||
current = mStart;
|
current = mStart;
|
||||||
@@ -219,15 +219,15 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
notifyChange();
|
notifyChange();
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyChange() {
|
protected void notifyChange() {
|
||||||
if (mListener != null) {
|
if (mListener != null) {
|
||||||
mListener.onChanged(this, mPrevious, mCurrent);
|
mListener.onChanged(this, mPrevious, mCurrent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateView() {
|
protected void updateView() {
|
||||||
|
|
||||||
/* If we don't have displayed values then use the
|
/* If we don't have displayed values then use the
|
||||||
* current number else find the correct value in the
|
* current number else find the correct value in the
|
||||||
* displayed values for the current number.
|
* displayed values for the current number.
|
||||||
@@ -239,7 +239,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
}
|
}
|
||||||
mText.setSelection(mText.getText().length());
|
mText.setSelection(mText.getText().length());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateCurrentView(CharSequence str) {
|
private void validateCurrentView(CharSequence str) {
|
||||||
int val = getSelectedPos(str.toString());
|
int val = getSelectedPos(str.toString());
|
||||||
if ((val >= mStart) && (val <= mEnd)) {
|
if ((val >= mStart) && (val <= mEnd)) {
|
||||||
@@ -253,7 +253,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onFocusChange(View v, boolean hasFocus) {
|
public void onFocusChange(View v, boolean hasFocus) {
|
||||||
|
|
||||||
/* When focus is lost check that the text field
|
/* When focus is lost check that the text field
|
||||||
* has valid values.
|
* has valid values.
|
||||||
*/
|
*/
|
||||||
@@ -280,12 +280,12 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
* to inform us when the long click has ended.
|
* to inform us when the long click has ended.
|
||||||
*/
|
*/
|
||||||
public boolean onLongClick(View v) {
|
public boolean onLongClick(View v) {
|
||||||
|
|
||||||
/* The text view may still have focus so clear it's focus which will
|
/* 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.
|
* trigger the on focus changed and any typed values to be pulled.
|
||||||
*/
|
*/
|
||||||
mText.clearFocus();
|
mText.clearFocus();
|
||||||
|
|
||||||
if (R.id.increment == v.getId()) {
|
if (R.id.increment == v.getId()) {
|
||||||
mIncrement = true;
|
mIncrement = true;
|
||||||
mHandler.post(mRunnable);
|
mHandler.post(mRunnable);
|
||||||
@@ -295,22 +295,22 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancelIncrement() {
|
public void cancelIncrement() {
|
||||||
mIncrement = false;
|
mIncrement = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancelDecrement() {
|
public void cancelDecrement() {
|
||||||
mDecrement = false;
|
mDecrement = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final char[] DIGIT_CHARACTERS = new char[] {
|
private static final char[] DIGIT_CHARACTERS = new char[] {
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
||||||
};
|
};
|
||||||
|
|
||||||
private NumberPickerButton mIncrementButton;
|
private NumberPickerButton mIncrementButton;
|
||||||
private NumberPickerButton mDecrementButton;
|
private NumberPickerButton mDecrementButton;
|
||||||
|
|
||||||
private class NumberPickerInputFilter implements InputFilter {
|
private class NumberPickerInputFilter implements InputFilter {
|
||||||
public CharSequence filter(CharSequence source, int start, int end,
|
public CharSequence filter(CharSequence source, int start, int end,
|
||||||
Spanned dest, int dstart, int dend) {
|
Spanned dest, int dstart, int dend) {
|
||||||
@@ -331,7 +331,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NumberRangeKeyListener extends NumberKeyListener {
|
private class NumberRangeKeyListener extends NumberKeyListener {
|
||||||
|
|
||||||
// XXX This doesn't allow for range limits when controlled by a
|
// XXX This doesn't allow for range limits when controlled by a
|
||||||
@@ -339,12 +339,12 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
public int getInputType() {
|
public int getInputType() {
|
||||||
return InputType.TYPE_CLASS_NUMBER;
|
return InputType.TYPE_CLASS_NUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected char[] getAcceptedChars() {
|
protected char[] getAcceptedChars() {
|
||||||
return DIGIT_CHARACTERS;
|
return DIGIT_CHARACTERS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence filter(CharSequence source, int start, int end,
|
public CharSequence filter(CharSequence source, int start, int end,
|
||||||
Spanned dest, int dstart, int dend) {
|
Spanned dest, int dstart, int dend) {
|
||||||
@@ -381,21 +381,21 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
|
|||||||
return Integer.parseInt(str);
|
return Integer.parseInt(str);
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < mDisplayedValues.length; i++) {
|
for (int i = 0; i < mDisplayedValues.length; i++) {
|
||||||
|
|
||||||
/* Don't force the user to type in jan when ja will do */
|
/* Don't force the user to type in jan when ja will do */
|
||||||
str = str.toLowerCase();
|
str = str.toLowerCase();
|
||||||
if (mDisplayedValues[i].toLowerCase().startsWith(str)) {
|
if (mDisplayedValues[i].toLowerCase().startsWith(str)) {
|
||||||
return mStart + i;
|
return mStart + i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The user might have typed in a number into the month field i.e.
|
/* The user might have typed in a number into the month field i.e.
|
||||||
* 10 instead of OCT so support that too.
|
* 10 instead of OCT so support that too.
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(str);
|
return Integer.parseInt(str);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
|
|
||||||
/* Ignore as if it's not a number we don't care */
|
/* Ignore as if it's not a number we don't care */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user