2
我有以下要求。使用Java模式API在EdiText上過濾條件
用戶只能在Android EditText上輸入以下範圍內的任何值,該值接受數字和小數而不是負數。
最小值:0
最大值:10個
中間值(最大值和最小值之間)可以是0.1,0.2,0.3 9.8 .....,9.9,10
我已經使用了Java模式來實現。
int digitsBeforeZero = 2;
int digitsAfterZero = 1;
public class DecimalDigitsInputFilter implements InputFilter {
private Pattern mPattern;
public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
mPattern = Pattern.compile(
"[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\\.)?");
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Matcher matcher = mPattern.matcher(dest);
if (!matcher.matches())
return "";
return null;
}
}
上述代碼適用於以下條件。
i>對於值10及以上,它不接受任何小數,這是正確的。
ii>它接受小數點後的一個數字,範圍爲0.0 - 9.9,這也是正確的。
但它接受來自0,0.1,0.2 9.8 .....,9.9,10,11,12 99 ...........
如何限制值以值10與現有條件?
任何建議將有所幫助。
當我添加此代碼作爲輸入過濾器,我無法對的EditText – chiranjib
添加任何東西請檢查我的更新 –
有了更新的代碼,我無法進入像1.2的值 – chiranjib