仿支付寶、微信EditText金額輸入驗證
阿新 • • 發佈:2019-02-01
金額輸入要考慮的幾種情況:
**1.首位輸入小數點的情況
2.小數點後保留2位
3.當切換小數點位置時的處理
4.不能輸入多個小數點。**
不多說, 上程式碼:
public class CashierInputFilter implements InputFilter {
Pattern mPattern;
//輸入的最大金額
private static final int MAX_VALUE = 100000;
//小數點後的位數
private static final int POINTER_LENGTH = 2;
private static final String POINTER = ".";
private static final String ZERO = "0";
private EditText et;
public CashierInputFilter(EditText edittext) {
et = edittext;
mPattern = Pattern.compile("([0-9]|\\.)*");
}
/**
* @param source 新輸入的字串
* @param start 新輸入的字串起始下標,一般為0
* @param end 新輸入的字串終點下標,一般為source長度-1
* @param dest 輸入之前文字框內容
* @param dstart 原內容起始座標,一般為0
* @param dend 原內容終點座標,一般為dest長度-1
* @return 輸入內容
*/
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String sourceText = source.toString();
String destText = dest.toString();
//驗證刪除等按鍵
if (TextUtils.isEmpty(sourceText)) {
if (dstart == 0 && destText.indexOf(POINTER) == 1) {//保證小數點不在第一個位置
return "0";
}
return "";
}
Matcher matcher = mPattern.matcher(source);
//已經輸入小數點的情況下,只能輸入數字
if (destText.contains(POINTER)) {
if (!matcher.matches()) {
return "";
} else {
if (POINTER.equals(source)) { //只能輸入一個小數點
return "";
}
}
//驗證小數點精度,保證小數點後只能輸入兩位
int index = destText.indexOf(POINTER);
int length = destText.trim().length() - index;
if (length > POINTER_LENGTH && dstart > index) {
return "";
}
} else {
//沒有輸入小數點的情況下,只能輸入小數點和數字,但首位不能輸入小數點和0
if (!matcher.matches()) {
return "";
} else {
if ((POINTER.equals(source))) {
if (dstart == 0 && dstart == destText.length())//首位輸入.的情況
return "0.";
if (dstart == 0) {//第一個位置輸入小數點的情況
try {
StringBuffer sb = new StringBuffer(destText);
sb.insert(dstart, "0.");
/* String left = destText.substring(0,dstart);
String right = destText.substring(dstart+1,destText.length());*/
double money = Double.valueOf(sb.toString());
et.setText(MoneyUtil.MoneyFomatWithTwoPoint(money));
et.setFilters(new InputFilter[]{this});
et.setSelection(dstart + 1);
return "";
} catch (NumberFormatException e) {
return source;
}
} else if (dstart == destText.length()) {//不在最後一個位置
return POINTER;
} else {//中間保留小數點後2位
try {
StringBuffer sb = new StringBuffer(destText);
sb.insert(dstart, POINTER);
double money = Double.valueOf(sb.toString());
et.setText(MoneyUtil.MoneyFomatWithTwoPoint(money));
et.setFilters(new InputFilter[]{this});
et.setSelection(dstart + 1);
return "";
} catch (NumberFormatException e) {
return source;
}
}
}
//第一位輸入0後面只能為.
if (destText.equals(ZERO) && !sourceText.equals(POINTER)) {
return "";
}
}
}
//驗證輸入金額的大小
double sumText = Double.parseDouble(destText + sourceText);
if (sumText > MAX_VALUE) {
return dest.subSequence(dstart, dend);
}
return dest.subSequence(dstart, dend) + sourceText;
}
}
為輸入金額的Edittext新增Filter,需要將EdiText物件匯入Filter,為切換小數點時進行金額驗證。
public class MoneyUtil {
public static String MoneyFomatWithTwoPoint(double d){
DecimalFormat df = new DecimalFormat("#.##");
return df.format(d);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.edittext);
InputFilter[] filters = {new CashierInputFilter(editText)};
editText.setFilters(filters);
}
}