大佬教程收集整理的这篇文章主要介绍了android – EditText随时显示2位小数的数字,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
除此之外,我也希望在字段为空而不是空的时候显示为零.
我已经将inputtype设置为:
android:inputType="number|numberdecimal"/>@H_502_7@我应该在这里使用inputfilters吗?
对不起我还是很新的android / java …
谢谢你的帮助!
编辑2011-07-09 23.35 – 解决第1部分2:“”到0.00.
有了nickfox的答案,我能够解决我的一半问题.
et.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s,int start,int count,int after) {} public void ontextChanged(CharSequence s,int before,int count) { if(s.toString().matches("")) { et.setText("0.00"); SELEction.setSELEction(et.getText(),4); } } });@H_502_7@我还在为另一半的问题找一个解决方案.如果我找到解决方案,我也会在这里发布.
编辑2011-07-09 23.35 – 解决第2部分2:将用户输入更改为具有两位小数的数字.
OnFocuschangelistener FocusChanged = new OnFocuschangelistener() { @Override public void onFocusChange(View v,Boolean hasFocus) { if(!hasFocus){ String userInput = et.getText().toString(); int dotPos = -1; for (int i = 0; i < userInput.length(); i++) { char c = userInput.charAt(i); if (c == '.') { dotPos = i; } } if (dotPos == -1){ et.setText(userInput + ".00"); } else { if ( userInput.length() - dotPos == 1 ) { et.setText(userInput + "00"); } else if ( userInput.length() - dotPos == 2 ) { et.setText(userInput + "0"); } } } }@H_502_7@
amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY); amountEditText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s,int after) {} public void ontextChanged(CharSequence s,int count) { if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$")) { String userInput= ""+s.toString().replaceAll("[^\\d]",""); StringBuilder cashamountBuilder = new StringBuilder(userInput); while (cashamountBuilder.length() > 3 && cashamountBuilder.charAt(0) == '0') { cashamountBuilder.deleteCharAt(0); } while (cashamountBuilder.length() < 3) { cashamountBuilder.insert(0,'0'); } cashamountBuilder.insert(cashamountBuilder.length()-2,'.'); cashamountBuilder.insert(0,'$'); amountEditText.setText(cashamountBuilder.toString()); // keeps the cursor always to the right SELEction.setSELEction(amountEditText.getText(),cashamountBuilder.toString().length()); } } });@H_502_7@
以上是大佬教程为你收集整理的android – EditText随时显示2位小数的数字全部内容,希望文章能够帮你解决android – EditText随时显示2位小数的数字所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。