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

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

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

問題描述

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

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

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

相關(guān)文檔推薦

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中的屏幕旋轉(zhuǎn)對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設(shè)置為僅接受 Android 中的數(shù)值?)
主站蜘蛛池模板: 久久久婷婷 | 999久久久久久久久6666 | 亚洲精品乱码久久久久久按摩观 | 精品国产欧美一区二区 | 在线观看第一页 | 日本电影免费完整观看 | 99亚洲精品视频 | 免费欧美 | 国产精品一区二区三区四区 | 91精品国产91久久久久游泳池 | 国产精品99| 国产激情一区二区三区 | 91av在线看| 毛片日韩 | 成人免费看黄 | 国产午夜精品一区二区三区在线观看 | 日韩精品一区二区三区中文字幕 | 欧美国产日韩一区 | 亚洲一区国产精品 | 久久久久久91 | 欧美成年黄网站色视频 | 日韩视频在线免费观看 | 天天干天天玩天天操 | 国产日韩一区二区 | 久久久精彩视频 | 在线观看免费观看在线91 | 免费观看成人av | 亚洲精品一区二区冲田杏梨 | 国产精品免费一区二区 | 欧美激情精品久久久久 | 国产美女自拍视频 | 成年人黄色小视频 | 国产ts人妖系列高潮 | 99热国产在线播放 | 国产成人精品久久二区二区 | 亚洲一区二区三区欧美 | 成人午夜精品 | 国产成人免费视频网站高清观看视频 | 国产aa| 九九99九九精彩46 | 69亚洲精品 |