简体   繁体   English

EditText 输入金额

[英]EditText to Input Money Amount

I am developing a Point Of Sales app.我正在开发销售点应用程序。

So I would like to let user to enter the purchase amount所以我想让用户输入购买金额

  1. Let's say User input 100000 but I want it to automatically show up 100,000 .假设用户输入100000但我希望它自动显示100,000 and 1000000 become 1,000,000 1000000变成1,000,000

  2. The second problem is that, I don't want user to be able to input .第二个问题是,我不希望用户能够输入. themselves.他们自己。

  3. Third problem is that since this is money, we can't let user to enter 0 in the beginning.第三个问题,既然是钱,我们不能让用户一开始就输入0。

Any ideas?有任何想法吗?

So far I can only come up with inputType=numberDecimal which is not really helpful.到目前为止,我只能提出inputType=numberDecimal这并不是很有帮助。

Thank you very much非常感谢

PS: I do not need any decimal places PS:我不需要任何小数位

if you want use in currency add addTextChangedListener to your desired edittext then monitor changes and reformat it , here is sample code如果你想在货币中使用addTextChangedListener添加到你想要的编辑文本然后监视更改并重新格式化它,这里是示例代码

private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if(!s.toString().equals(current)){
       [your_edittext].removeTextChangedListener(this);

       String cleanString = s.toString().replaceAll("[$,.]", "");

       double parsed = Double.parseDouble(cleanString);
       String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

       current = formatted;
       [your_edittext].setText(formatted);
       [your_edittext].setSelection(formatted.length());

       [your_edittext].addTextChangedListener(this);
    }
}

You could implement one or more InputFilter s to enforce those constraints on your EditText .您可以实现一个或多个InputFilter以在您的EditText上强制执行这些约束。 It is possible to attach multiple filters on an EditText by using it's setFilters method.可以使用setFilters方法在EditText上附加多个过滤器。

You could also use TextWatcher s to achieve the same thing.您也可以使用TextWatcher来实现相同的目的。 However, using InputFilter makes a little bit more sense as it allows you to alter the text without having to call the setText method after each change you make in the input.但是,使用InputFilter更有意义,因为它允许您在每次更改输入后更改文本而无需调用setText方法。

For your first problem follow this link Thousand separator对于您的第一个问题,请按照此链接千位分隔符

For your second problem add this to your editext对于您的第二个问题,将此添加到您的 editext

android:digits="0123456789"
android:inputType="numberDecimal"

And for your third problem you have to use TextWatcher like this对于你的第三个问题,你必须像这样使用 TextWatcher

editText1.addTextChangedListener(new TextWatcher(){
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if (editText1.getText().toString().matches("^0") )
        {
            // Not allowed
            Toast.makeText(context, "not allowed", Toast.LENGTH_LONG).show();
            editText1.setText("");
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM