实时编辑后的And​​r​​oid的EditText如何格式化号码 [英] How to format number in android EditText after realtime editing

查看:167
本文介绍了实时编辑后的And​​r​​oid的EditText如何格式化号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText中,用户必须输入一个号码,包括小数,我想一个千位分隔符自动添加到输入号码,我尝试了几个其他的方法,但某些不允许浮点数,所以我想出了这个code效果很好,只表示字符串输入未编辑的实时一个可能的千位分隔符和错误似乎从s.replace()来干;

  AM2 =新TextWatcher(){
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
    }
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
    公共无效afterTextChanged(编辑S){
        如果(s.toString()。等于()){
            amount.setText();
            值= 0;
        }其他{
            StringBuffer的strBuff =新的StringBuffer();
            焦炭℃;
            的for(int i = 0; I< amount2.getText()的toString()长度();我++){
                。C = amount2.getText()的toString()的charAt(ⅰ);
                如果(Character.isDigit(c))的{
                    strBuff.append(C);
                }
            }
            值= Double.parseDouble(strBuff.toString());
            相反();
            NumberFormat的NF2 = NumberFormat.getInstance(Locale.ENGLISH);
            ((DecimalFormat的)NF2).applyPattern(###,### #######。);
            s.replace(0,s.length(),nf2.format(值));
        }
    }
};
 

解决方案

本类解决了这个问题,允许小数点输入,并增加了千位分隔符。

 公共类NumberTextWatcher实现TextWatcher {

私人DecimalFormat的DF;
私人DecimalFormat的dfnd;
私人布尔hasFractionalPart;

私人的EditText等;

公共NumberTextWatcher(的EditText等)
{
    DF =新的DecimalFormat(#,###。##。);
    df.setDecimalSeparatorAlwaysShown(真正的);
    dfnd =新的DecimalFormat(#,###);
    this.et =等;
    hasFractionalPart = FALSE;
}

@燮pressWarnings(未使用)
私有静态最后字符串变量=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;
    }
}
 

}

来源:<一href="http://blog.roshka.com/2012/08/android-edittext-with-number-format.html">http://blog.roshka.com/2012/08/android-edittext-with-number-format.html

I have an EditText in which the user should input a number including decimals and i want a thousand separator automatically added onto the input number I tried a couple of other methods but some do not allow floating point numbers so i came up with this code which works well only that the string input is not being edited in realtime to one with possible thousand separators and the errors seem to stem from the s.replace();

    am2 = new TextWatcher(){
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
    public void afterTextChanged(Editable s) {
        if (s.toString().equals("")) {
            amount.setText("");
            value = 0;
        }else{
            StringBuffer strBuff = new StringBuffer();
            char c;
            for (int i = 0; i < amount2.getText().toString().length() ; i++) {
                c = amount2.getText().toString().charAt(i);
                if (Character.isDigit(c)) {
                    strBuff.append(c);
                }
            }
            value = Double.parseDouble(strBuff.toString());
            reverse();
            NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
            ((DecimalFormat)nf2).applyPattern("###,###.#######");
            s.replace(0, s.length(), nf2.format(value));
        }
    }
};

解决方案

This Class solves the problem, allows decimal input and adds the thousand separators.

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";

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

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().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
    {
        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }
}

}

Source: http://blog.roshka.com/2012/08/android-edittext-with-number-format.html

这篇关于实时编辑后的And​​r​​oid的EditText如何格式化号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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