Android版的EditText:格式化数 [英] Android edittext: Formating number

查看:392
本文介绍了Android版的EditText:格式化数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText ,如果我输入输入编号为 100000 ,其结果必然是 100.000 ,如果我输入的输入为百万,其结果必然是 1,000,000

在从最后开始,每3个字符必须有

下面是我的code:

  tambah =(EditText上)rootView.findViewById(R.id.total);
tambah.setOnFocusChangeListener(新OnFocusChangeListener(){
       公共无效onFocusChange(视图V,布尔hasFocus){
              // ...
       }


解决方案

下面是我用它来对美元的输入。它确保没有任何时刻都只有2位过去小数点。您应该能够以使其适应你的需要通过删除$符号。

  amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
amountEditText.addTextChangedListener(新TextWatcher(){
    公共无效afterTextChanged(编辑S){}
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){}公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    如果(s.toString()的比赛(^ \\\\ $(\\\\Ð{1,3}(\\\\ \\\\ð{3})* |!(\\\\ D +))(\\\\。 ð{2})?$))
    {
        字符串userInput =+ s.toString()的replaceAll([^ \\\\ D]。,);
        StringBuilder的cashAmountBuilder =新的StringBuilder(userInput);        而(cashAmountBuilder.length()→3&放大器;&放大器; cashAmountBuilder.charAt(0)=='0'){
            cashAmountBuilder.deleteCharAt(0);
        }
        而(cashAmountBuilder.length()3;){
            cashAmountBuilder.insert(0,'0');
        }
        cashAmountBuilder.insert(cashAmountBuilder.length() - 2,'。');
        cashAmountBuilder.insert(0,'$');        amountEditText.setText(cashAmountBuilder.toString());
        //保持光标总是向右
        Selection.setSelection(amountEditText.getText(),cashAmountBuilder.toString()长度());    }}
});

I have a edittext, and if I enter input number as 100000, the result must be 100.000, and if I enter input as 1000000, the result must be 1.000.000.

After every 3 characters from the last to beginning must have a "."

Here is my code:

tambah = (EditText)rootView.findViewById(R.id.total);
tambah.setOnFocusChangeListener(new OnFocusChangeListener(){
       public void onFocusChange(View v, boolean hasFocus) {
              // ...
       }

解决方案

Here is something I use to for dollar input. It makes sure that there are only 2 places past the decimal point at all times. You should be able to adapt it to your needs by removing the $ sign.

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());

    }

}
});

这篇关于Android版的EditText:格式化数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆