本文介绍了普通的前pression帮助输入过滤了的EditText在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个输入过滤,格式为 1234.35 限制数字输入。也就是说,在此之前最多四个。键,小数点后两位。我用这模式:

I need to implement an input filter for limiting numeral entry in the format 1234.35. That is, maximum four before . and two decimal places. I am using this regular expression pattern:

Pattern.compile("[0-9]{0,4}+((\\.[0-9]{0,2})?)||(\\.)?");

这工作,但一旦我在编辑文本输入号码,并尝试之前,小数编辑值,我无法对其进行编辑。我只能将其删除。

This works, but once I enter a number in the edit text and try to edit the values before the decimal places, I can't edit them. I can only delete them.

什么是错的?

推荐答案

根据您所说的,我认为它看起来像你试图做的,我会用这个普通的前pression:

Based upon what you said and I think it looks like you were trying to do, I would use this regular expression:

^(\d{0,4})(\.\d{1,2})?$

这0-4个数字'匹配带或不带一个小数点和两个号码跟了上去。如果有小数点,那么一个或者两个数字必须遵循它。例如: 5 1234 1234.56 0.2 0.31 都是有效的,并由前pression匹配,但 0.123 1234 1234.567 12345 不匹配。

It matches '0-4 digits' with or without 'a decimal point and two numbers' following them. If there is a decimal point, then either one or two digits must follow it. For instance: 5, 1234, 1234.56, .2, and .31 are all valid and matched by the expression, but .123, 1234., 1234.567, 12345, and . are NOT matched.

另外,允许数以小数结尾(如 1234 ,等),使用这种修改

Alternatively, to allow numbers ending in decimals (like ., 1234., and the like), use this modification:

^(\d{0,4})(\.(\d{1,2})?)?$

这篇关于普通的前pression帮助输入过滤了的EditText在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:31