如何在android EditText中添加千位分隔符.........?

时间:2017-01-19 05:53:18

标签: android android-edittext

我想像这样分开自动编辑文本,如(9,99,999)。我在网上搜索了这个,但我没有得到适当的解决方案。 你可以帮帮我吗。谢谢你堆叠溢出。

4 个答案:

答案 0 :(得分:1)

您可以使用DecimalFormat,如下面的代码:

public String formatNumber(double d) {

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

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

        return formatter.format(d);
    }

您可以根据需要传递图案。

答案 1 :(得分: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;
    }

}

}

答案 2 :(得分:0)

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

        }
        return number;
    }

这就是我所做的。完美运作

答案 3 :(得分:0)

  editText.addTextChangedListener(new TextWatcher() {
   @Override
   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
   }
   @Override
   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
   }
   @Override
   public void afterTextChanged(Editable editable) {
   editText.removeTextChangedListener(this);
   try {
StringBuilder originalString = new StringBuilder(editable.toString().replaceAll(",", ""));
int indx = 0;
for (int i = originalString.length(); i > 0; i--) {
if (indx % 3 == 0 && indx > 0)
 originalString = originalString.insert(i, ",");
 indx++;
}
 editText.setText(originalString);
 editText.setSelection(originalString.length());
  } catch (NumberFormatException nfe) {
  nfe.printStackTrace();
  }
                    editText.addTextChangedListener(this);
                }
            });