EditText限制小数点前后位数的实例
作者:jingxian 时间:2023-04-10 03:56:14
在EditText输入数字的时候,通常我们需要限制小数点前后位数。比如金额输入一般我们需要限制小数点后面最多2位。我们可以通过 TextWatcher 实现。
public class MyWatcher implements TextWatcher {
private int beforeDot;
private int afterDot;
/**
* 构造器
*
* @param beforeDot 小数点前位数 不限制输入-1
* @param afterDot 小数点后位数 不限制输入-1
*/
public MyWatcher(int beforeDot, int afterDot) {
this.beforeDot = beforeDot;
this.afterDot = afterDot;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
judge(s);
}
private void judge(Editable editable) {
String temp = editable.toString();
int posDot = temp.indexOf(".");
//直接输入小数点的情况
if (posDot == 0) {
editable.insert(0, "0");
return;
}
//连续输入0
if (temp.equals("00")) {
editable.delete(1, 2);
return;
}
//输入"08" 等类似情况
if (temp.startsWith("0") && temp.length() > 1 && (posDot == -1 || posDot > 1)) {
editable.delete(0, 1);
return;
}
//不包含小数点 不限制小数点前位数
if (posDot < 0 && beforeDot == -1) {
//do nothing 仅仅为了理解逻辑而已
return;
} else if (posDot < 0 && beforeDot != -1) {
//不包含小数点 限制小数点前位数
if (temp.length() <= beforeDot) {
//do nothing 仅仅为了理解逻辑而已
} else {
editable.delete(beforeDot, beforeDot + 1);
}
return;
}
//如果包含小数点 限制小数点后位数
if (temp.length() - posDot - 1 > afterDot && afterDot != -1) {
editable.delete(posDot + afterDot + 1, posDot + afterDot + 2);//删除小数点后多余位数
}
}
}
使用的时候 如下:
<EditText
android:id="@+id/et_i_want"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:hint="0.00"
android:inputType="numberDecimal"/>etIWant.addTextChangedListener(new MyWatcher(-1, 2));//限制小数点后最多2位
标签:edittext,限制,小数点
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android百度定位导航之基于百度地图移动获取位置和自动定位
2022-10-21 10:50:18
![](https://img.aspxhome.com/file/2023/5/129015_0s.png)
Java中泛型的示例详解
2023-10-27 02:54:24
Android Webview的postUrl与loadUrl加载页面实例
2021-06-26 19:39:04
MyBatis常用标签以及使用技巧总结
2022-02-27 20:52:14
OpenCV实现反阈值二值化
2022-06-29 16:02:33
![](https://img.aspxhome.com/file/2023/6/76316_0s.jpg)
mybatis查询语句揭秘之参数解析
2021-07-10 01:17:18
C#导出数据到CSV文件的通用类实例
2023-05-22 09:23:16
Spring Boot 实现图片上传并回显功能
2021-10-11 17:45:20
![](https://img.aspxhome.com/file/2023/6/68776_0s.png)
Android如何通过手机自动获取短信验证码
2022-12-10 00:19:47
![](https://img.aspxhome.com/file/2023/6/106816_0s.png)
Android使用lottie加载json动画的示例代码
2021-06-20 00:47:01
![](https://img.aspxhome.com/file/2023/6/138656_0s.png)
Unity实现鼠标或者手指点击模型播放动画
2023-11-30 23:06:01
![](https://img.aspxhome.com/file/2023/4/72494_0s.jpg)
详解Java线程同步器CountDownLatch
2023-08-23 18:42:39
![](https://img.aspxhome.com/file/2023/2/58342_0s.png)
add方法理解ArrayList的扩容机制
2023-11-24 02:16:28
MyBatis图文并茂讲解注解开发多对多查询
2022-10-27 17:05:04
![](https://img.aspxhome.com/file/2023/1/67991_0s.png)
Mybatis plus中的like查询问题
2022-03-18 09:14:30
Android开发实现判断通知栏是否打开及前往设置页面的方法
2022-10-21 10:43:45
c# socket编程udp客户端实现代码分享
2023-06-16 05:03:31
Java内存溢出实现原因及解决方案
2022-10-19 09:07:42
![](https://img.aspxhome.com/file/2023/6/129176_0s.png)
spring mvc中注解@ModelAttribute的妙用分享
2022-09-15 13:53:25
c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的区别详解
2023-01-26 04:59:46