EditText限制小数点后两位
来源:互联网 发布:2016云计算大会 编辑:程序博客网 时间:2024/06/05 15:09
1 只允许输入小数点后面最多两位(editText监听,用时直接将editText传入即可)
public static void setPricePoint(final EditText editText) {editText.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before, int count) {if (s.toString().contains(".")) {if (s.length() - 1 - s.toString().indexOf(".") > 2) {s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);editText.setText(s);editText.setSelection(s.length());}}if (s.toString().trim().substring(0).equals(".")) {s = "0" + s;editText.setText(s);editText.setSelection(2);}if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {if (!s.toString().substring(1, 2).equals(".")) {editText.setText(s.subSequence(0, 1));editText.setSelection(1);return;}}}public void beforeTextChanged(CharSequence s, int start, int count, int after) {}public void afterTextChanged(Editable s) {}});}
2获得小数点后面位数长度(可以在监听事件afterTextChanged方法中,监听输入后的小数点长度)。如下et_length便是小数点后面长度。
@Override
public void afterTextChanged(Editable s) {
Stringmoney = editText.getText().toString();
BigDecimalbigDecimal = new BigDecimal(String.valueOf(money));
et_length = bigDecimal.scale();
Log.e("AAA", "length = " + et_length);
}
3 EditText只允许输入数字和小数点类型
<EditTextandroid:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
/>
0 0
- EditText限制小数点后两位
- EditText输入限制小数点后两位
- EditText限制小数点前后位数
- EditText限制小数点前后位数
- EditText金额输入(限制输入金额大小和小数点后两位)
- Android EditText限制小数点输入两位
- android EditText 中的TextWatcher 限制小数点位数
- EditText限制输入的小数点只能一个
- Android 限制EditText输入小数点后面位数
- EditText限制输入小数点后字符位数
- EditText保留小数点后两位
- 封装的EditText的输入限制(输入的数字前+¥,精确到小数点后两位)
- Android EditText保留小数点后两位
- EditText限制输入的小数点只能2个
- Android EditText控制小数点位数及输入限制工具类
- 利用正则表达式限制EditText小数点前后位数和格式
- 限制输入小数点后两位数字
- 输入框限制小数点 后两位
- 【LeetCode OJ 015】3Sum
- 【FastDev4Android框架开发】RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout(三十一)
- iOS.swift 记录常见的修饰符
- JSP中getParameter与getAttribute区别
- 环信代码分析笔记1
- EditText限制小数点后两位
- java 获取当前日期 分析当前日期星期几
- 谈谈Memcached与Redis
- WebService之axis1.x的应用
- 关于Your account already has a valid certificate
- canvas
- HTML5
- arduino编译过程
- hdu 1102 (最小生成树)