我会使用正则表达式的功能在编辑文本本身中进行过滤。首先是正则表达式:
^-?(d{0,5}|d{0,5}.d{0,3})$也许有多种方法可以改善这种表达方式,但这确实可以解决问题。
现在,只需在edittext中设置输入过滤器,如下所示:
final String regex = "^-?(d{0,5}|d{0,5}.d{0,3})$";((EditText)rootView.findViewById(R.id.editText1)).setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) { if (end > start) { // adding: filter // build the resulting text String destinationString = destination.toString(); String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd); // return null to accept the input or empty to reject it return resultingTxt.matches(regex) ? null : ""; } // removing: always accept return null; } }});顺便说一句,我刚刚测试了这段代码,它的作用是:
- 用户最多可以输入8位数字。
- 用户输入“。”后,允许的最大十进制数字为8。
我是否正确理解您描述的问题?
-编辑
好吧,我快到了。据我了解,decimal(8,3)表示最多8位数字,包括小数点左边或右边的数字,范围从
-99999.999到
99999.999。我至少从这句话中了解到这一点
StandardSQL requires that DECIMAL(5,2) be able to store any value with five digits andtwo decimals, so values that can be stored in the salary column range from-999.99 to 999.99。即使来自MySQL文档,MSSQL文档似乎也可以做到这一点。



