Edittext判断输入是否为数字(包含小数点)
2016-12-12 18:39
411 查看
在开发中EditText总会要求输入限制,数字?个数?几行?
1.在限制输入类型为double的数字时就需要做两步判断,
在布局中定义EditText的时候设置为只能输入数字,设置数字的时候可以直接设置android:numeric="decimal" ;
然后再进行具体判断
2.限制输入小数位数
需要给EditText设置TextWatcher,在输入的时候进行判断
希望能帮助到小伙伴们……
1.在限制输入类型为double的数字时就需要做两步判断,
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:numeric="decimal" />
在布局中定义EditText的时候设置为只能输入数字,设置数字的时候可以直接设置android:numeric="decimal" ;
然后再进行具体判断
/** * 判断字符串是否是数字 */ public static boolean isNumber(String value) { return isInteger(value) || isDouble(value); } /** * 判断字符串是否是整数 */ public static boolean isInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } /** * 判断字符串是否是浮点数 */ public static boolean isDouble(String value) { try { Double.parseDouble(value); if (value.contains(".")) return true; return false; } catch (NumberFormatException e) { return false; } }
2.限制输入小数位数
需要给EditText设置TextWatcher,在输入的时候进行判断
TextWatcher mTextWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { String temp = s.toString(); int posDot = temp.indexOf("."); if (posDot <= 0) return; if (temp.length() - posDot - 1 > 4) { s.delete(posDot + 5, posDot + 6); } } };
希望能帮助到小伙伴们……
相关文章推荐
- JS实现网页全屏与退出全屏
- iScroll上拉加载下拉刷新 -> 遇到的问题
- 第三周的第一天,心情很down。
- nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
- 学习小记-MySQL
- CoordinatorLayout使用中的坑
- 数据结构::矩阵(一)--对称矩阵及对称矩阵的压缩存储
- k8s入门系列之介绍篇
- 使用 Redis 实现排行榜功能 (转载 https://segmentfault.com/a/1190000002694239)
- iOS开发ARC内存管理技术要点
- 字符串反转,单词反转
- Apache curator-client详解
- CSRF跨站点请求伪造漏洞问题
- Sanic 连接postgresql数据库
- Linux查看进程打开多少文件描述符命令-lsof
- java 缓冲
- 如何组建测试团队?
- 敏感字符串加密处理(PHP实现)
- java字典序全排列
- 循序渐进Python3(十一) --6-- Ajax 实现跨域请求 jsonp 和 cors