android给EditText加金额的属性,带元,角和分,如¥12.34
2016-03-18 13:51
417 查看
功能如标题,代码如下: (本代码
可直接用到你的项目中,只需要将EditText transactionMoney替换成你的即可)
String abc = ""; EditText transactionMoney;//就是随便一个输入框,表示金额 //下面就是给editText加一个文字监听
transactionMoney.addTextChangedListener(new TextWatcher() { @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) { if (!s.toString().equals(abc)) { transactionMoney.removeTextChangedListener(this); String replaceable = String.format("[%s, \\s.]", NumberFormat.getCurrencyInstance(Locale.CHINA).getCurrency().getSymbol(Locale.CHINA)); String cleanString = s.toString().replaceAll(replaceable, ""); if (cleanString.equals("") || new BigDecimal(cleanString).toString().equals("0")) { transactionMoney.setText(null); 4000 } else { double parsed = Double.parseDouble(cleanString); String formatted = NumberFormat.getCurrencyInstance(Locale.CHINA).format((parsed / 100)); abc = formatted; transactionMoney.setText(formatted); transactionMoney.setSelection(formatted.length()); } transactionMoney.addTextChangedListener(this); } } });
希望对您有所帮助!
相关文章推荐
- 输入数量和价格自动计算输出金额的js代码
- Android2.3实现Android4.0风格EditText的方法
- Android中EditText+Button组合导致输入板无法收起的原因分析及解决办法
- Android GridView中包含EditText的焦点重新获取方法
- Android编程开发之EditText中不输入特定字符会显示相关提示信息的方法
- 解决EditText、ListView以及GridView同时使用,输入法自动跳出来的方法
- Android如何自定义EditText下划线?
- Android编程开发之EditText中inputType属性小结
- android同时控制EditText输入字符个数和禁止特殊字符输入的方法
- Android实现EditText控件禁止输入内容的方法(附测试demo)
- Android中EditText实现不可编辑解决办法
- Android编程实现在自定义对话框中获取EditText中数据的方法
- Android中EditText如何去除边框添加下划线
- Android中EditText光标在4.0中的bug及解决方法
- Android实现EditText中添加和删除bitmap的方法
- Android编程实现监听EditText变化的方法
- Android文本输入框(EditText)输入密码时显示与隐藏
- Android编程中EditText限制文字输入的方法
- Android开发中给EditText控件添加TextWatcher监听实现对输入字数的限制(推荐)
- android基础教程之android的listview与edittext冲突解决方法