? ? 小菜最近因工作需要做一個支付金額對話框的小模塊,技術很淺顯,遇到幾個小坑,分享給大家~
小菜遇到的坑之一:
? ? 其他頁面中獲取到的金額需要保存兩位小數,本來我打算直接用 String 轉為 double 類型,但是遇到保存精度問題(String=19.99 ->Double=19.98),所以借鑒BigDecimal 方式,如圖:
小菜遇到的坑之二:
? ? 只可保留兩位小數,即有兩位小數后再輸入不顯示;若先輸入一個長度,例如1234,移動光標,再在1234之間點小數點,只可有12.34或123.4或1234.而不可是1.234
小菜遇到的坑之三:
? ? 時時判斷輸入金額不可大于10000,自己用了一個很low的方法:
直接上關鍵代碼吧? (依舊很low,僅供借鑒)
final EditText amountEt = ButterKnife.findById(view, R.id.pay_view_amount_tv);
Double amountDouble = Double.parseDouble(amount);
BigDecimal bignum = new BigDecimal(amountDouble);
amountEt.setText(bignum.setScale(2, BigDecimal.ROUND_HALF_UP) + "");
amountEt.setFilters(new InputFilter[]{new InputFilter() {
@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)) {
? ? ? ? ? ? ? ? ? return "";
? ? ? ? }
? ? ? ? mPattern = Pattern.compile("([0-9]|\\.)*");
? ? ? ? Matcher matcher = mPattern.matcher(source);
? ? ? ? // 已經輸入小數點的情況下,只能輸入數字
? ? ? ? if (destText.contains(".")) {
? ? ? ? ? ? ? ? if (!matcher.matches()) { ?? return ""; ? ?? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? if (".".equals(source)) {? //只能輸入一個小數點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? return "";
? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?? }
? ? ? ?? double sumText = Double.parseDouble(destText + sourceText);
? ? ? ? if (sumText > 10000) { ? ?? return dest.subSequence(dstart, dend); ? ?? }
? ? ? ? //驗證小數點精度,保證小數點后只能輸入2位
? ? ?? int index = destText.indexOf(".");
? ? ?? int length = dend - index;
? ? ?? if (dest.toString() != null && dest.toString().length() > 0 && dest.toString().contains(".")) {
? ? ? ? ? ? ? String[] amoArr = (dest.toString()).split("\\.");
? ? ? ? ? ? ? int indexPoint = dest.toString().indexOf(".");
? ? ? ? ? ?? if (dstart <= indexPoint) {
? ? ? ? ? ? ? ? ? ?? if (dend > 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String temp = amoArr[0].substring(0, dend - 1) + sourceText + amoArr[0].substring(dend - 1, amoArr[0].length());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (Double.parseDouble(temp) > 10000) { ? ? ? return ""; ? ? ?? }
? ? ? ? ? ? ? ? ?? }
? ? ? ? ? } else if (amoArr.length > 1 && amoArr[amoArr.length - 1].length() >= 2) { ? ? return ""; ? ? }
? ? }
? ?? if (length > 10) { ? ?? return dest.subSequence(dstart, dend); ? ? }
? ?? } else { // 還沒有輸入小數點.的情況
? ? ? ? ? ? ? if (source.equals(".") && dest.toString().length() == 0) { ? ? return "0."; ?? } else {
? ? ? ? ? ?? if (dest.toString() != null && dest.toString().length() > 0) {
? ? ? ? ? ? ? ? ? ? ?? double sumDou = Double.parseDouble(dest.toString());
? ? ? ? ? ? ? ? ? ? ?? if (sumDou > 10000) { ? ? ? return ""; ? ? }
? ? ? ? ? ? ? }
? ? ?? String[] amoArr = (dest.toString()).split("\\.");
? ? ? int indexPoint = dest.toString().indexOf(".");
if (dstart <= indexPoint) {
if (dest.toString() != null && dest.toString().length() > 0) {
double sumDou = Double.parseDouble(dest.toString());
if (sumDou > 10000) {
return dest.subSequence(dstart, dend);
}
}
} else if (amoArr.length > 1 && amoArr[amoArr.length - 1].length() >= 2) {
return "";
} else if (source.equals(".") && dest.toString().length() - dstart > 2) {
return "";
}
}
}
if ((destText + sourceText).length() > 10) {
return dest.subSequence(dstart, dend);
}
//驗證輸入金額的大小
double sumText = Double.parseDouble(destText + sourceText);
if (sumText > 10000) {
return dest.subSequence(dstart, dend);
}
return dest.subSequence(dstart, dend) + sourceText;
}
}});
// 關閉贊賞提示對話框
ImageView imgClose = ButterKnife.findById(view, R.id.pay_view_close_iv);
imgClose.setOnClickListener(new View.OnClickListener() {
? ? ?? @Override
? ? ?? public void onClick(View v) {
? ? ? ? ? ?? materialDialog.dismiss();
? ? ?? }
});
// 確定贊賞
TextView tvPaySure = ButterKnife.findById(view, R.id.pay_view_sure_tv);
tvPaySure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amountEt.getText().toString().trim();
if (!StringUtils.isBlank(amount)) {
if (amount.contains(",")) {
amount.replaceAll("\\,", "");
String[] amoArr = amount.split("\\,");
String tempAmoStr = "";
for (String i : amoArr) {
tempAmoStr = tempAmoStr + i;
}
amount = tempAmoStr;
}
double tempAmo = Double.valueOf(amount);
if (tempAmo <= 0.00) {
ToastUtils.showShort(mContext, "請輸入一定金額");
} else if (tempAmo > 0.00 && tempAmo <= 9999.99) {
materialDialog.dismiss();
if (getAccountInfo() != null) {
payCommon();
} else {
startActivity(new Intent(mContext, LoginActivity.class));
}
} else {
ToastUtils.showShort(mContext, "不要超過一萬");
}
} else {
ToastUtils.showShort(mContext, "請輸入一定金額");
}
}
});
// 取消贊賞
TextView tvPayCancel = ButterKnife.findById(view, R.id.pay_view_cancel_tv);
tvPayCancel.setOnClickListener(new View.OnClickListener() {
? ? ?? @Override
? ? ?? public void onClick(View v) {
? ? ? ? ? ? ? materialDialog.dismiss();
? ? ?? }
});