2012-08-10 131 views
1

您好朋友我正在使用以下代码显示以货币形式输入的金额。INR货币格式问题

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, '$'); 


       editAmount.setText(cashAmountBuilder.toString()); 
       editAmount.setTextKeepState(cashAmountBuilder.toString()); 
       Selection.setSelection(editAmount.getText(), cashAmountBuilder.toString().length()); 
      } 
     } 

问题的符号前缀为“$”我要么用新的INR符号替换或空白。我试图用空白替换cashAmountBuilder.insert(0, '$');补丁代码,它给了我编译错误。请帮助我一样。 谢谢,

+0

你得到了什么编译时错误? – 2012-08-10 06:58:51

+0

@AndroSelva字符常量无效 – onkar 2012-08-10 07:00:08

+0

但是我尝试这一行时没有遇到任何编译时错误?你确定? – 2012-08-10 07:02:59

回答

0

伙计们找到了这个解决方案。想与大家分享。

editAmount=(EditText)findViewById(R.id.editTextPaymentAmount); 
     mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); 
     editAmount.setRawInputType(Configuration.KEYBOARD_12KEY); 
     editAmount.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().equals(current)){ 
         editAmount.removeTextChangedListener((TextWatcher) this); 
       // String replaceable = String.format("[%s,.]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol()); 
       //  String cleanString = s.toString().replaceAll(replaceable, ""); 
        String cleanString = s.toString().replaceAll("[$,.]", ""); 
       // String cleanString = s.toString(). replaceAll("[^\\d]", ""); 
         BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);     
         String formato = NumberFormat.getCurrencyInstance().format(parsed); 
    System.out.println(formato); 
         current = formato; 
         String newSt=formato.substring(1); 

         editAmount.setText(newSt); 
         editAmount.setSelection(newSt.length()); 

         editAmount.addTextChangedListener((TextWatcher) this); 
        } 
       } 
      }); 
    } 

public void priceClick(View view) { 
     editAmount.addTextChangedListener(new TextWatcher(){ 
      DecimalFormat dec = new DecimalFormat("0.00"); 
      private String current = ""; 
      public void afterTextChanged(Editable arg0) { 
      } 

      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().equals(current)){ 
        editAmount.removeTextChangedListener((TextWatcher) this); 

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

        BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);     
        String formato = NumberFormat.getCurrencyInstance().format(parsed); 

        current = formato; 
        editAmount.setText(formato); 
        editAmount.setSelection(formato.length()); 

        editAmount.addTextChangedListener((TextWatcher) this); 
       } 
      } 
     }); 
    }