小菜最近因工作需要做一个支付金额对话框的小模块,技术很浅显,遇到几个小坑,分享给大家~
需求
小菜遇到的坑之一:其他页面中获取到的金额需要保存两位小数,本来我打算直接用 String 转为 double 类型,但是遇到保存精度问题(String=19.99 ->Double=19.98),所以借鉴BigDecimal 方式,如图:
String->Double精度转换
小菜遇到的坑之二:只可保留两位小数,即有两位小数后再输入不显示;若先输入一个长度,例如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) {
if ((destText + sourceText).length() > 10) {
//验证输入金额的大小
double sumText = Double.parseDouble(destText + sourceText);
if (sumText > 10000) {
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() {
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));
ToastUtils.showShort(mContext, "不要超过一万");
// 取消赞赏
TextView tvPayCancel = ButterKnife.findById(view, R.id.pay_view_cancel_tv);
tvPayCancel.setOnClickListener(new View.OnClickListener() {
materialDialog.dismiss();
效果图