如何从正确类型的十进制值(钱)向左? [英] How to type decimal values (money) from right to left?

查看:125
本文介绍了如何从正确类型的十进制值(钱)向左?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,当一个场被加载时,它是空的。当用户键入1,它说0.01,那么他的另一个类型0 1(10)之后,就变成0.10。当他类型另一个1,就变成1.10

Suppose when a field is loaded, its empty. When the user types 1, it says 0.01, then he types another 0 after the 1 (10) it becomes 0.10. When he types another 1, it becomes 1.10.

就像进入了一个产品的价格。我如何做到这一点在Android?我很为难,说实话,因为我从来没有尝试过这样的事情。是否有一些特殊的输入法呢?

Just like entering the price of a product. How do I do that on Android? I'm quite stumped to be honest since I've never tried something like this. Is there some special input method for this?

推荐答案

确定家伙..我有一个临时的解决方法。这不能称为完美的解决方案,但它是一个解决办法,在我的情况下,效果很好。

OK guys.. I have a temporary workaround. This can't be termed as the perfect solution but its a workaround that works well in my case.

我无法想出一个办法来阻止计算器错误。我的解决方法吗?使现场的EditText获取输入,但显示货币输出到一个TextView,并从UI上的EditText消失,使用户无法看到他打字的只是1234,而不是12.34。

I couldn't figure out a way to stop the stackoverflow error. My workaround? Make the EditText field take input, but display the currency output onto a TextView, and make the EditText disappear from the UI so the user can't see that he is typing just 1234 instead of 12.34.

下面是code。该addCurrencySign功能来自@ ss1271。

Here is the code. The addCurrencySign function comes from @ss1271.

amountpaid.addTextChangedListener(new TextWatcher()
        {

            @Override public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                amount.setText(addCurrencySign(s.toString()));
            }

            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override public void afterTextChanged(Editable s)
            {

            }
        });

下面是用户发布的功能。它工作得很好。

Here's the function that the user posted. It works very well.

private String addCurrencySign(String digits)
    {
        String string = "&"; // Your currency
        // Amount length greater than 2 means we need to add a decimal point
        if (digits.length() > 2)
        {
            String pound = digits.substring(0, digits.length() - 2); // Pound
                                                                        // part
            String pence = digits.substring(digits.length() - 2); // Pence part
            string += pound + "." + pence;
        }
        else if (digits.length() == 1)
        {
            string += "0.0" + digits;
            Log.d("TextWatcher", "length 1 " + string);
        }
        else if (digits.length() == 2)
        {
            string += "0." + digits;
            Log.d("TextWatcher", "length 2 " + string);
        }

        return string;
    }

这里的无形的EditText:

Here's the 'invisible' edittext:

<EditText
        android:id="@+id/amoundpaid"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:background="#00000000"
        android:ems="10"
        android:inputType="number"
        android:textColor="#00ffffff" >

        <requestFocus />

下面是将显示输出文本(用货币符号和正确的小数点值)字段:

Here's the field that will show the output text (with currency sign and proper decimal point value):

<TextView
        android:id="@+id/amount"
        android:layout_width="210dp"
        android:layout_height="50dp"
        android:background="#80000000"
        android:gravity="center|center_vertical"
        android:textColor="#ffffff"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

我知道这是不是一个最佳的解决方案,但它的工作原理。我需要的功能,这是我必须做的就是它。如果有人能解决这个计算器错误,请张贴和MODS将答案变成一个(一旦其验证)。

I know this is not a optimum solution, but it works. I needed the functionality and this is what I had to do to get it. If someone can fix the stackoverflow error, please post and mods will change the answer to that one (once its verified).

这篇关于如何从正确类型的十进制值(钱)向左?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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