Fixed some bugs in the time picker--leading zeroes, resetting after typing manually

pull/14/head
Sam Bosley 13 years ago
parent 8068759c6f
commit 4923f3fd89

@ -137,8 +137,8 @@ public class AstridTimePicker extends LinearLayout {
amButton.setChecked(false);
pmButton.setChecked(false);
} else {
hours.updateView();
minutes.updateView();
hours.validateAndUpdate();
minutes.validateAndUpdate();
amButton.setChecked(!lastSelectionWasPm);
pmButton.setChecked(lastSelectionWasPm);

@ -24,4 +24,8 @@ public class DeadlineNumberPicker extends NumberPicker {
return R.layout.deadline_number_picker;
}
@Override
protected int getMaxDigits() {
return 2;
}
}

@ -23,6 +23,7 @@ import android.content.Context;
import android.os.Handler;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@ -123,6 +124,14 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
return R.layout.number_picker;
}
/**
* @return The number of allowable digits that can be typed in (-1 for unlimited)
* e.g. return 2 if you don't want to allow 00002 even if 2 is in range.
*/
protected int getMaxDigits() {
return -1;
}
public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
setOrientation(VERTICAL);
@ -294,12 +303,22 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
}
}
private void validateCurrentView(CharSequence str) {
public void validateAndUpdate() {
String str = String.valueOf(mText.getText());
if (TextUtils.isEmpty(str)) {
updateView();
} else {
validateCurrentView(str, false);
}
}
private void validateCurrentView(CharSequence str, boolean notifyChange) {
int val = getSelectedPos(str.toString());
if ((val >= mStart) && (val <= mEnd)) {
mPrevious = mCurrent;
mCurrent = val;
notifyChange(mCurrent);
if (notifyChange)
notifyChange(mCurrent);
}
updateView();
}
@ -318,7 +337,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
} else {
// Check the new value and ensure it's in range
validateCurrentView(str);
validateCurrentView(str, true);
}
}
}
@ -404,6 +423,10 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
if ("".equals(result)) {
return result;
}
if (getMaxDigits() > 0 && result.length() > getMaxDigits())
return "";
int val = getSelectedPos(result);
/*
@ -463,7 +486,7 @@ public class NumberPicker extends LinearLayout implements OnClickListener,
*/
public int getCurrent() {
String str = String.valueOf(((TextView) mText).getText());
validateCurrentView(str);
validateCurrentView(str, true);
return mCurrent;
}
}
Loading…
Cancel
Save