2017-01-19 9 views

答えて

0

これを試してください。

public class NumberTextWatcherForThousand implements TextWatcher { 

EditText editText; 


public NumberTextWatcherForThousand(EditText editText) { 
    this.editText = editText; 

} 

@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) { 
    try 
    { 
     editText.removeTextChangedListener(this); 
     String value = editText.getText().toString(); 


     if (value != null && !value.equals("")) 
     { 

      if(value.startsWith(".")){ //adds "0." when only "." is pressed on begining of writting 
       editText.setText("0."); 
      } 
      if(value.startsWith("0") && !value.startsWith("0.")){ 
       editText.setText(""); //Prevents "0" while starting but not "0." 

      } 


      String str = editText.getText().toString().replaceAll(",", ""); 
      if (!value.equals("")) 

      editText.setText(getDecimalFormat(str)); 
      editText.setSelection(editText.getText().toString().length()); 
     } 
     editText.addTextChangedListener(this); 
     return; 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
     editText.addTextChangedListener(this); 
    } 

} 

public static String getDecimalFormat(String value) 
{ 
    StringTokenizer lst = new StringTokenizer(value, "."); 
    String str1 = value; 
    String str2 = ""; 
    if (lst.countTokens() > 1) 
    { 
     str1 = lst.nextToken(); 
     str2 = lst.nextToken(); 
    } 
    String str3 = ""; 
    int i = 0; 
    int j = -1 + str1.length(); 
    if (str1.charAt(-1 + str1.length()) == '.') 
    { 
     j--; 
     str3 = "."; 
    } 
    for (int k = j;; k--) 
    { 
     if (k < 0) 
     { 
      if (str2.length() > 0) 
       str3 = str3 + "." + str2; 
      return str3; 
     } 
     if (i == 3) 
     { 
      str3 = "," + str3; 
      i = 0; 
     } 
     str3 = str1.charAt(k) + str3; 
     i++; 
    } 

} 


//Trims all the comma of the string and returns 
public static String trimCommaOfString(String string) { 

    if(string.contains(",")){ 
     return string.replace(",","");} 
    else { 
     return string; 
    } 

} 

}

+0

私は新しいクラスを作成したいright – chandu

+0

いいえ、あなたはこのNumberTextWatcherForThousandをあなたのEditTextのaddTextChangedListener –

1

あなたはコード以下のように、このためにはDecimalFormatを使用することができますあなたが望むよう

public String formatNumber(double d) { 

     DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US); 

     formatter.applyPattern("#,###"); 

     return formatter.format(d); 
    } 

あなたはパターンを渡すことができます。

0
public static String formatCurrency(String number) { 
     try { 
      number = NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(number)); 
     } catch (Exception e) { 

     } 
     return number; 
    } 

これは私がしたものです。完全に動作します

関連する問題