我已提到link
设置允许输入的编辑文本的最小值和最大值。但我想将最小值和最大值修正为双倍值。最小值应允许从0.25开始输入,最大值应允许1000.0。如何解决?
答案 0 :(得分:0)
也许尝试定义EditText.setOnFocusChangedListener
:
public abstract void onFocusChange (View v, boolean hasFocus){
if(v.getId() == (yourEditTextID) && hasFocus == false){
//Check your editTextValue
//if not in range, show an error (Toast ?) and refocus
}
}
答案 1 :(得分:0)
您可以使用输入过滤器。这是我的解决方案
public class MinMaxInputFilter implements InputFilter {
private double mMinValue;
private double mMaxValue;
private static final double MIN_VALUE_DEFAULT = Double.MIN_VALUE;
private static final double MAX_VALUE_DEFAULT = Double.MAX_VALUE;
public MinMaxInputFilter(Double min, Double max) {
this.mMinValue = (min != null ? min : MIN_VALUE_DEFAULT);
this.mMaxValue = (max != null ? max : MAX_VALUE_DEFAULT);
}
public MinMaxInputFilter(Integer min, Integer max) {
this.mMinValue = (min != null ? min : MIN_VALUE_DEFAULT);
this.mMaxValue = (max != null ? max : MAX_VALUE_DEFAULT);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String replacement = source.subSequence(start, end).toString();
String newVal = dest.subSequence(0, dstart).toString() + replacement
+ dest.subSequence(dend, dest.length()).toString();
// check if there are leading zeros
if (newVal.matches("0\\d+.*"))
if (TextUtils.isEmpty(source))
return dest.subSequence(dstart, dend);
else
return "";
// check range
double input = Double.parseDouble(newVal);
if (!isInRange(mMinValue, mMaxValue, input))
if (TextUtils.isEmpty(source))
return dest.subSequence(dstart, dend);
else
return "";
return null;
} catch (NumberFormatException nfe) {
LOGE("inputfilter", "parse");
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
如何使用它:
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
InputFilter limitFilter = new MinMaxInputFilter(mMinValue, mMaxValue);
editText.setFilters(new InputFilter[] { limitFilter });
答案 2 :(得分:0)
太晚但效果很好:
package com.test;
import android.text.InputFilter;
import android.text.Spanned;
/**
* Created by Admin on 22.04.2016.
*/
public class InputFilterMinMax implements InputFilter {
private int min, max;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
double input = Double.valueOf(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
用法:
et = (EditText) findViewById(id.editText);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("0", "100")});
答案 3 :(得分:-2)
package com.test;
import android.text.InputFilter;
import android.text.Spanned;
public class InputFilterMinMax implements InputFilter {
private double min, max;
public InputFilterMinMax(double min, double max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Double.parseInt(min);
this.max = Double.parseInt(max);
}
@Override
public CharSequence filter(CharSequence source, double start, double end, Spanned dest, double dstart, double dend) {
try {
double input = Double.parseDouble(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}