0

當按下點號或逗號按鈕時,按下了inputType =「number」或inputType =「number | Decimal」。它未能與android:digits =「。」,一起使用。無法使用EditText輸入小數點inputType =「number」Android

EditText包含一個格式化數字的textwatcher。文字如下:

mEditWithdrawalAmount.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) { 

     if (!s.toString().equals(current)) { 
      mEditWithdrawalAmount.removeTextChangedListener(this); 

      String replaceable = String.format("[%s .\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol()); 
      String cleanString = s.toString().replaceAll(replaceable, "").replace("R","").replace(",",""); 
      double parsed; 
      try { 
       parsed = Double.parseDouble(cleanString); 
      } catch (NumberFormatException e) { 
       parsed = 0.00; 
      } 

      String formatted = Utils.formatCurrency(parsed); 

      current = formatted; 
      mEditWithdrawalAmount.setText(formatted); 
      mEditWithdrawalAmount.setSelection(formatted.length()); 

      // Do whatever you want with position 
      mEditWithdrawalAmount.addTextChangedListener(this); 
     } 
    } 
}); 

問題是edittext必須允許帶小數點的數字。

  • 實際結果是:R1000 000
  • 期望的結果是R1000 000.00或R1000 000.40
+0

在XML editext安卓的inputType = 「numberDecimal」 –

+0

安卓的inputType = 「numberDecimal」 只有工作沒有onTextChange監聽 – BigWiz

回答

0

嘿檢查此代碼。

android:inputType="numberDecimal" 

希望得到這個幫助。

+0

不與Android合作:的inputType = 「numberDecimal」 – BigWiz

+0

你能更具體你想要什麼? –

0

您可以使用輸入濾波器是這樣的:

InputFilter filter = new InputFilter() { 
     public CharSequence filter(CharSequence source, int start, int end, 
            Spanned dest, int dstart, int dend) { 
      if(source.equals("")){ // for backspace 
       return source; 
      } 

      if(source.toString().matches("[0-9.]+")){ 
       return source; 
      } 
      return ""; 
     } 
    }; 

然後將其設置爲您編輯文本

topedittext.setFilters(new InputFilter[] { filter,new InputFilter.LengthFilter(30) }); 
0

嘗試了這一點: 這是一個示例代碼

amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY); 
    amountEditText.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 start, int before, 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()); 

      } 

     } 
    }); 

或這一個它適用於我

我已經實現了onFocusChangedListener中的所有內容。還要確保將EditText輸入類型設置爲「number | numberDecimal」。

更改如下:如果輸入爲空,則用「0.00」替換。如果輸入有兩個以上的精度小數,則投下兩位小數。一些小的重構。

editText.setOnFocusChangeListener(new OnFocusChangeListener() { 
@Override public void onFocusChange(View v, boolean hasFocus) { 
    if (!hasFocus) { 
     String userInput = ET.getText().toString(); 

     if (TextUtils.isEmpty(userInput)) { 
      userInput = "0.00"; 
     } else { 
      float floatValue = Float.parseFloat(userInput); 
      userInput = String.format("%.2f",floatValue); 
     } 

     editText.setText(userInput); 
    } 
} 
}); 
相關問題