EditText十进制值 [英] EditText decimal value

查看:108
本文介绍了EditText十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现EditText,并且遇到一个问题...我已经说过inputTypenumberDecimal,但是在我键入EditText时喜欢放."或,"自动... 我可以把100.0090.00视用户而定...可以吗?

I'm implementing EditText's and I'm facing with one issue... I've put that the inputType is numberDecimal but at the time I type on the EditText I'd like to put the "." or "," autommatically... I can put for example 100.00 or 90.00 it depends of the user... Can it be possible?

我试图创建一个TextWatcher并在onTextChanged上做一些事情,例如查看charSequence-lenght()==2是否添加一个".或,"但不起作用...

I tried to create a TextWatcher and do some things on onTextChanged like see if the charSequence-lenght()==2 add a "." or "," but doesn't work...

即使我使用##.#####.##这样的十进制格式,我也可以检测何时放入逗号或点吗?正如我所说的,它可以是100.0080.00.

Can I detect when to put the comma or dot even if I have a decimal format like ##.## or ###.## ? As I said it can be 100.00 or 80.00.

 @Override
    public void afterTextChanged(Editable editable) {
        //et1 and et2 are not empty
        if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){
            double pe1 , pe2;
            pe1 = Double.parseDouble(et1.getText().toString());
            pe2 = Double.parseDouble(et2.getText().toString());
            double res = (pe1+pe2)/2;
            String formattedString = String.format("%,.2f", res);
            tvPr.setText(formattedString);

        }

问题出在不是afterTextChanged的textInput上,我想...因为我必须输入.".手动操作,我想避免这种情况.

The problem is on the textInput not on afterTextChanged I guess... because I have to put the "." manually and I want to avoid this.

我的一个朋友也向我建议,我可以不理会.和,由于editText上的标记始终为100、80、32等...,只需键入这些数字,然后将其数字化为双精度即可,当用户完成editText的编辑时,我想输入.00那可能吗?

Also a friend of mine suggested to me that I could ignore the . and , and since the marks are always 100, 80, 32, etc... on the editText just type those numbers but then tract those as a double and I'd like to put a ,00 when the user finish the editting of editText is that possible?

比较if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){当时是否很好,检查edittext是否不为空?一旦两个edittext都不为空,我必须更新TextView并进行一些操作,因此,我尝试了这种方法,还有另一种更好的方法吗? (我把lenght()== 5放进去,因为在EditText上我把maxlenght()= 5;

Are the comparation if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){ good at the time to check wheter is an edittext not empty? I have to update a TextView once both edittext are not empty and do some operations so, I tried this way, is there another one better? (I put lenght()==5 because on EditText I put maxlenght() = 5;

推荐答案

您要做的就是这样

final DecialFormat df = new DecimalFormat("#.00");

input.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) {
        String value = s.toString();
        if(TextUtils.isEmpty(value)){
            // do what you want if it is empty
            return;
        }

        try {
            DecimalFormat df = new DecimalFormat("#.00");
            String testFormat = df.format(Double.parseDouble(s.toString()));

            // Manage the cursor position
            int newPos;
            try {
                int pos = et.getSelectionStart();
                int sizeDiff = testFormat.length() - s.length();
                newPos = pos + sizeDiff;
            }catch(Exception e1){
                e1.printStackTrace();
                newPos = testFormat.length();
            }

            // Modify the edit text to hold the double value
            et.removeTextChangedListener(this);
            et.setText(testFormat);
            et.setSelection(newPos);
            et.addTextChangedListener(this);
        }catch(Exception e){
            e.printStackTrace();
            et.removeTextChangedListener(this);
            et.setText("");
            et.addTextChangedListener(this);
        }
    }
}

修改上面的代码以执行所需的操作,但这是确保值是十进制形式的基本思想.值得一提的是,当您像这样修改文本时,光标位置始终是尝试获得所需内容的有趣"事物.如果您有"100.00"并且有人将光标放在"100.0 | 0"之类并按5,该怎么办?结果应为"100.0 | 5"还是"100.05 |"如果是后者,您将如何处理它们输入下一个值,是否使用它舍入前一个值?这是一个有趣的情况,但这会帮助您入门.

Modify the above code to do what you need, but this is the basic idea behind making sure the value is of decimal form. It is worth mentioning that when you modify text like this the cursor position is always a "fun" thing to try to get how you want it. What happens if you have "100.00" and someone puts the cursor in a place like "100.0|0" and presses 5. Should the result be "100.0|5" or "100.05|" and if it is the latter how do you handle them putting in the next value, do you use it to round the previous value? It is an interesting situation, but this will get you started.

这篇关于EditText十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆