久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

實時編輯后如何在android EditText中格式化數字

How to format number in android EditText after realtime editing(實時編輯后如何在android EditText中格式化數字)
本文介紹了實時編輯后如何在android EditText中格式化數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 EditText,用戶應該在其中輸入一個包含小數的數字,我希望在輸入數字上自動添加一個千位分隔符我嘗試了其他幾種方法,但有些方法不允許浮點數,所以我想出了這個僅在字符串輸入未實時編輯為具有可能的千位分隔符且錯誤似乎源于 s.replace();

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

}

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

這篇關于實時編輯后如何在android EditText中格式化數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 久久久久国产精品 | 亚洲免费网 | 91视频电影 | 精品国产精品国产偷麻豆 | 91精品久久久久久久久 | 日韩一区二区在线免费观看 | 国产精品91视频 | 91精品一区| 全免费a级毛片免费看视频免费下 | 亚洲字幕在线观看 | 999久久久 | 国产一区三区视频 | 欧美一级全黄 | 黄网站涩免费蜜桃网站 | 国产精品久久久一区二区三区 | 国产精品高清一区二区三区 | 午夜专区| 91最新在线视频 | 欧美日韩国产传媒 | 久久精品二区 | 欧美日韩亚洲一区 | 久久三级av | 欧美精品网站 | 久草视频在线播放 | 精品av天堂毛片久久久借种 | av天天干| 国产激情一区二区三区 | 在线免费亚洲视频 | 黄色网址免费在线观看 | av在线免费不卡 | 99资源| 在线视频h | 精品一区二区久久久久久久网站 | 中文字幕视频在线免费 | 午夜在线免费观看 | 狠狠爱一区二区三区 | www九色| 特黄级国产片 | 国产精品视频一二三区 | 欧美一区二区三区久久精品 | 成人av一区|