मैं एक स्वच्छ समाधान here भर में आ गए हैं:
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 {
int input = Integer.parseInt(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
और बस इतना की तरह एक पाठ संपादित करने के लिए इस फ़िल्टर को लागू:
mCentsEditText = (EditText)v.findViewById(R.id.cents_edit_text);
InputFilterMinMax filter = new InputFilterMinMax("0", "99") {};
mCentsEditText.setFilters(new InputFilter[]{filter});
स्रोत
2014-05-15 11:26:41
इस सवाल कई बार पढ़ने के बाद, यह है अस्पष्ट है कि क्या उपयोगकर्ता टाइपिंग करते समय इनपुट को सत्यापित और संपादित करना चाहते हैं, या फ़ील्ड से बाहर निकलने के बाद। इन दोनों मामलों में से आपके पते के नीचे आपके दो जवाब हैं। – Devunwired