Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – EditText随时显示2位小数的数字大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想随时显示两个小数的EditText字段的输入.所以当用户输入5时,会显示5.00或当用户输入7.5时显示7.50.

除此之外,我也希望在字段为空而不是空的时候显示为零.

我已经将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@

解决方法

这是我用于美元输入的东西.它确保始终只有2个小数点.您应该能够通过删除$符号来适应您的需求.
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,请注明来意。