如何与千位分隔符(,)在Android中打字的时候格式化的EditText的输入? [英] How to format the input of EditText when typing with thousands separators (,) in Android?

查看:244
本文介绍了如何与千位分隔符(,)在Android中打字的时候格式化的EditText的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText ,它只是没有十进制数得到的数值。

 的android:inputType下=数字

我想在我打字分隔千。例如25000人。

我知道我应该使用 TextWatcher 键,我用这个code,但我不能让它工作:

  @覆盖
        公共无效afterTextChanged(编辑viewss){
            字符串s = NULL;
            尝试{
                //格式说明逗号的伎俩
                S =的String.format(%,D,的Long.parseLong(viewss.toString()));
            }赶上(NumberFormatException的E){
            }        }

您能帮我做到这一点?


解决方案

测试这个例子:

 进口java.text.DecimalFormat中;
进口java.text.ParseException;进口android.text.Editable;
进口android.text.TextWatcher;
进口android.widget.EditText;公共类NumberTextWatcher实现TextWatcher {    私人DecimalFormat的DF;
    私人DecimalFormat的dfnd;
    私人布尔hasFractionalPart;    私人的EditText等;    公共NumberTextWatcher(的EditText等)
    {
        DF =新的DecimalFormat(#,###。##。);
        df.setDecimalSeparatorAlwaysShown(真);
        dfnd =新的DecimalFormat(####);
        this.et =等;
        hasFractionalPart = FALSE;
    }    @燮pressWarnings(未使用)
    私有静态最后弦乐TAG =NumberTextWatcher;    @覆盖
    公共无效afterTextChanged(编辑S)
    {
        et.removeTextChangedListener(本);        尝试{
            INT inilen,endlen;
            。inilen = et.getText()长();            串V = s.toString()取代(将String.valueOf(df.getDecimalFormatSymbols()getGroupingSeparator()),);
            数n = df.parse(V);
            INT CP = et.getSelectionStart();
            如果(hasFractionalPart){
                et.setText(df.format(正));
            }其他{
                et.setText(dfnd.format(正));
            }
            。endlen = et.getText()长();
            INT SEL =(CP +(endlen - inilen));
            如果(SEL大于0&放大器;&放大器; SEL&下; = et.getText()长度()){
                et.setSelection(SEL);
            }其他{
                //把光标在结束了吗?
                et.setSelection(et.getText()长() - 1);
            }
        }赶上(NumberFormatException的NFE){
            // 没做什么?
        }赶上(ParseException的E){
            // 没做什么?
        }        et.addTextChangedListener(本);
    }    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT)
    {
    }    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)
    {
        如果(s.toString()。包含(将String.valueOf(df.getDecimalFormatSymbols()。getDecimalSeparator())))
        {
            hasFractionalPart = TRUE;
        }其他{
            hasFractionalPart = FALSE;
        }
    }}


  

要使用它,一个TextChangedListener添加到的EditText组件。


  editText.addTextChangedListener(新NumberTextWatcher(EDITTEXT));

I've an edittext , it only gets numeric without decimal numbers.

android:inputType="number"

I want to separate thousands while I'm typing . For example 25,000 .

I know I should use TextWatcher and I've used this code but I couldn't make it work :

@Override
        public void afterTextChanged(Editable viewss) {
            String s = null;
            try {
                // The comma in the format specifier does the trick
                s = String.format("%,d", Long.parseLong(viewss.toString()));
            } catch (NumberFormatException e) {
            }

        }

Could you help me to do so ?

解决方案

Test this example:

import java.text.DecimalFormat;
import java.text.ParseException;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;

    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("#,###.##");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###");
        this.et = et;
        hasFractionalPart = false;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "NumberTextWatcher";

    @Override
    public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (hasFractionalPart) {
                et.setText(df.format(n));
            } else {
                et.setText(dfnd.format(n));
            }
            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
        {
            hasFractionalPart = true;
        } else {
            hasFractionalPart = false;
        }
    }

}

To use it, add a TextChangedListener to the EditText component.

editText.addTextChangedListener(new NumberTextWatcher(editText));

这篇关于如何与千位分隔符(,)在Android中打字的时候格式化的EditText的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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