【问题标题】:"EditText and TextView" formatted with thousands separators (,) in android“EditText 和 TextView”在 android 中用千位分隔符 (,) 格式化
【发布时间】:2014-11-07 12:51:01
【问题描述】:

当我在 editText 中输入时,我想要在 EditText 中使用千位分隔符 (,)。 之后,我将对数字进行一些操作。然后在 TextView 中以千位分隔符 (,) 显示结果。

这是我的代码:

public class Mainactivity extends Activity {

/** Called when the activity is first created. */

EditText        edt;
TextView        txt;
Button          btn;

OnClickListener myclick = new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {

                                // my calculation
                                double num1 = Double.parseDouble(edt.getText().toString());
                                double num2 = num1 + 7;

                                txt.setText("" + num2);

                            }
                        };


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edt = (EditText) findViewById(R.id.edt);
    txt = (TextView) findViewById(R.id.txt);
    btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(myclick);

}

}

【问题讨论】:

标签: android android-edittext textview separator


【解决方案1】:

你可以使用

String value=String.format("%,.2f", num );

将数字解析为带有千位分隔符的字符串,然后将结果设置为您想要的 TextView 或 EditText

例如:

txt.setText(String.format("%,.2f", num ));
edt.setText(String.format("%,.2f", num ));

【讨论】:

  • 这对 textView 很好,但对于 EditText,我想在输入值时分开。
【解决方案2】:

尝试使用此代码

        @Override
        public void afterTextChanged(Editable s) {

            try
            {
                edittext.removeTextChangedListener(this);
                String value = edittext.getText().toString();
                if (value != null && !value.equals(""))
                {
                    String str = edittext.getText().toString().replaceAll(",", "");
                    if (value != null && !value.equals(""))
                        Double.valueOf(str).doubleValue();
                    edittext.setText(getDecimalFormat(str));
                    edittext.setSelection(edittext.getText().toString().length());
                }
                edittex.addTextChangedListener(this);
                return;
            }
            catch (Exception localException)
            {
                localException.printStackTrace();
                edittext.addTextChangedListener(this);
            }
        }

 private 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++;
    }
}

希望能帮到你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-29
    • 2015-02-28
    • 2018-09-05
    • 2022-07-31
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    相关资源
    最近更新 更多