第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 TextWatcher Android 添加 3 個小數(shù)位貨幣格式到 edittext

如何使用 TextWatcher Android 添加 3 個小數(shù)位貨幣格式到 edittext

30秒到達(dá)戰(zhàn)場 2023-03-17 15:20:33
我想在開始時使用TextWatcher向EditText添加3 個小數(shù)位 貨幣格式,值為0.000并且數(shù)字應(yīng)該從右到左變化例如:如果我按 1、2、3、4、5 順序值應(yīng)顯示為12.345以下代碼僅適用于小數(shù)點后 2 位。請任何人幫助我如何將此代碼更改為小數(shù)點后 3 位或其他解決方案 public class CurrencyTextWatcher  implements TextWatcher {    boolean mEditing;    Context context;    public CurrencyTextWatcher() {        mEditing = false;    }    public synchronized void afterTextChanged(Editable s) {        if(!mEditing) {            mEditing = true;            String digits = s.toString().replaceAll("\\D", "");             NumberFormat nf = NumberFormat.getCurrencyInstance();            try{                String formatted = nf.format(Double.parseDouble(digits)/100);                s.replace(0, s.length(), formatted);            } catch (NumberFormatException nfe) {                s.clear();            }            mEditing = false;        }    }    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }    public void onTextChanged(CharSequence s, int start, int before, int count) { }}
查看完整描述

1 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗 獲得超10個贊

除以 1000 而不是 100,也setMinimumFractionDigits除以NumberFormat3。


public class CurrencyTextWatcher  implements TextWatcher {

    boolean mEditing;

    Context context;



    public CurrencyTextWatcher() {

        mEditing = false;

    }


    public synchronized void afterTextChanged(Editable s) {


        if(!mEditing) {

            mEditing = true;

            String digits = s.toString().replaceAll("\\D", "");

            NumberFormat nf = NumberFormat.getCurrencyInstance();

            nf.setMinimumFractionDigits(3);


            try{

                String formatted = nf.format(Double.parseDouble(digits)/1000);

                s.replace(0, s.length(), formatted);

            } catch (NumberFormatException nfe) {

                s.clear();

            }

            mEditing = false;

        }

    }


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


    public void onTextChanged(CharSequence s, int start, int before, int count) { }


}


查看完整回答
反對 回復(fù) 2023-03-17
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號