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() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
@ -153,11 +151,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override public void onFocusChange(View v, boolean hasFocus) {
focused = hasFocus;
updateEditText();
}
editText.setOnFocusChangeListener((v, hasFocus) -> {
focused = hasFocus;
if (!focused) getValue(); // check min/max
updateEditText();
});
}
@ -204,12 +201,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
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.minValue = minValue;
this.maxValue = maxValue;
this.step = step;
this.formatter = formater;
this.formatter = formatter;
this.allowZero = allowZero;
callValueChangedListener();
this.okButton = okButton;
@ -234,6 +231,14 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
}
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;
}