簡體   English   中英

EditText 輸入金額

[英]EditText to Input Money Amount

我正在開發銷售點應用程序。

所以我想讓用戶輸入購買金額

  1. 假設用戶輸入100000但我希望它自動顯示100,000 1000000變成1,000,000

  2. 第二個問題是,我不希望用戶能夠輸入. 他們自己。

  3. 第三個問題,既然是錢,我們不能讓用戶一開始就輸入0。

有任何想法嗎?

到目前為止,我只能提出inputType=numberDecimal這並不是很有幫助。

非常感謝

PS:我不需要任何小數位

如果你想在貨幣中使用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);
    }
}

您可以實現一個或多個InputFilter以在您的EditText上強制執行這些約束。 可以使用setFilters方法在EditText上附加多個過濾器。

您也可以使用TextWatcher來實現相同的目的。 但是,使用InputFilter更有意義,因為它允許您在每次更改輸入后更改文本而無需調用setText方法。

對於您的第一個問題,請按照此鏈接千位分隔符

對於您的第二個問題,將此添加到您的 editext

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

對於你的第三個問題,你必須像這樣使用 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