当前位置:网站首页>模仿微信金钱输入框规则(修复7.0手机崩溃)
模仿微信金钱输入框规则(修复7.0手机崩溃)
2022-08-09 14:55:00 【按劳分配】
微信转账输入框规则(可能不全)
1、小数点后两位
2、起始输入小数点,显示0.
3、删除到第一个位置是小数点的时候,第一个位置为0 ,避免出现小数点在第一个位置的情况
修改这个朋友的规则而来,他的规则在保证小数点后两位有个小bug,已经修改
http://blog.csdn.net/shineflowers/article/details/50846268
效果:

public class CashierInputFilter implements InputFilter {
Pattern mPattern;
Pattern mPattern2;
//输入的最大金额
private static final int MAX_VALUE = Integer.MAX_VALUE;
//小数点后的位数
private static final int POINTER_LENGTH = 2;
private static final String POINTER = ".";
private static final String ZERO = "0";
public CashierInputFilter() {
mPattern = Pattern.compile("([0-9]|\\.)*");
mPattern2 = Pattern.compile("(\\.)*");
}
/**
* @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();
try {
//验证删除等按键
if (TextUtils.isEmpty(sourceText)) {
if(dstart==0&&destText.indexOf(POINTER)==1){
return "0";
}
return "";
}
Matcher matcher = mPattern.matcher(source);
Matcher matcher2 = mPattern2.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 ((matcher2.matches()) && dstart==0) {
return "0.";
}
// if ((POINTER.equals(source)) && dstart==0) {
// return "0.";
// }
}
}
//验证输入金额的大小
double sumText = Double.parseDouble(destText + sourceText);
if (sumText > MAX_VALUE) {
return dest.subSequence(dstart, dend);
}
return dest.subSequence(dstart, dend) + sourceText;
} catch (NumberFormatException e) {
e.printStackTrace();
try {
return dest.subSequence(dstart, dend) + sourceText;
} catch (Exception e1) {
e1.printStackTrace();
return "";
}
}
}
}
修复:
1、7.0小米4手机对于输入的 . 和程序中的“.”无法 equal相等,估改为正则表达式匹配
EditText样式
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:maxLength="10"
android:inputType="numberDecimal"
/>
设置filter
EditText ed = (EditText) findViewById(R.id.et);
InputFilter[] is = {new CashierInputFilter()};
ed.setFilters(is);
如有bug,希望指出,谢谢。
边栏推荐
猜你喜欢

(12)Cookie和Session

记一次解决Mysql:Incorrect string value: ‘\xF0\x9F\x8D\x83\xF0\x9F...‘ for column 插入emoji表情报错问题

简单记录下offsetof和container_of

地铁预约Postman脚本使用
Example of file operations - downloading and merging streaming video files
![[Elementary C language] Detailed explanation of branch statements](/img/5c/9ae0fc48e021d1285badf295dda4be.png)
[Elementary C language] Detailed explanation of branch statements

Simply record offsetof and container_of

正则化原理的简单分析(L1/L2正则化)
It is deeply recognized that the compiler can cause differences in the compilation results

SNR signal-to-noise ratio
随机推荐
你知道亚马逊代运营的成本是多少吗?
【小白必看】初始C语言(下)
Qt控件-QTextEdit使用记录
Use tensorboard remotely on the server
The recycle bin has been showed no problem to empty the icon
模型训练的auc和loss比较问题
6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
How to create a new project with VS+Qt
For programming trading, focusing on forecast or on countermeasures?
NoUniqueBeanDefinitionException和JSON乱码处理出现异常
C语言运算符优先级
面试合集
Left-handed and Right-handed Binary Sorted Trees
ImageWatch无法显示图像
Introduction to OpenCV and build the environment
二叉排序树的左旋与右旋
对导入的 excel 的时间的处理 将excel表中的时间,转成 标准的时间
自定义指令,实现默认头像和用户上传头像的切换
如何正确使用防关联浏览器
众所周知亚马逊是全球最大的在线电子商务公司。如今,它已成为全球商品种类最多的在线零售商,日活跃买家约为20-25亿。另一方面,也有大大小小的企业,但不是每个人都能赚到刀! 做网店的同学都知道,