NumberPicker better check for allowed range

This commit is contained in:
Milos Kozak 2020-12-23 22:27:27 +01:00
parent 3be91d83f6
commit f0a96866af

View file

@ -132,12 +132,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
setTextWatcher(new TextWatcher() { setTextWatcher(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
@Override @Override
@ -153,11 +151,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
} }
}); });
editText.setOnFocusChangeListener(new OnFocusChangeListener() { editText.setOnFocusChangeListener((v, hasFocus) -> {
@Override public void onFocusChange(View v, boolean hasFocus) { focused = hasFocus;
focused = hasFocus; if (!focused) getValue(); // check min/max
updateEditText(); updateEditText();
}
}); });
} }
@ -204,12 +201,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
editText.addTextChangedListener(textWatcher); editText.addTextChangedListener(textWatcher);
} }
public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, Button okButton) { public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formatter, boolean allowZero, Button okButton) {
this.value = initValue; this.value = initValue;
this.minValue = minValue; this.minValue = minValue;
this.maxValue = maxValue; this.maxValue = maxValue;
this.step = step; this.step = step;
this.formatter = formater; this.formatter = formatter;
this.allowZero = allowZero; this.allowZero = allowZero;
callValueChangedListener(); callValueChangedListener();
this.okButton = okButton; this.okButton = okButton;
@ -234,6 +231,14 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
} }
public Double getValue() { public Double getValue() {
if (value > maxValue) {
value = maxValue;
ToastUtils.showToastInUiThread(getContext(), getContext().getString(R.string.youareonallowedlimit));
}
if (value < minValue) {
value = minValue;
ToastUtils.showToastInUiThread(getContext(), getContext().getString(R.string.youareonallowedlimit));
}
return value; return value;
} }