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

EditText 始終顯示帶 2 位小數(shù)的數(shù)字

EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
本文介紹了EditText 始終顯示帶 2 位小數(shù)的數(shù)字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我想始終以兩位小數(shù)顯示 EditText 字段的輸入.因此,當(dāng)用戶輸入 5 時將顯示 5.00,或者當(dāng)用戶輸入 7.5 時將顯示 7.50.

I would like to display the input of the EditText fields with two decimals at all times. So when the user enters 5 it will show 5.00 or when the user enters 7.5 it will show 7.50.

除此之外,我還想在字段為空而不是空時顯示零.

Besides that I would like to also show zero when the field is empty instead of nothing.

我已經(jīng)將輸入類型設(shè)置為:

What I've got already is the inputtype set to:

android:inputType="number|numberDecimal"/>

我應(yīng)該在這里使用輸入過濾器嗎?

Should I work with inputfilters here?

對不起,我對 android/java 還是很陌生...

Sorry I still quite new to android / java...

感謝您的幫助!

有了 nickfox 的回答,我的問題解決了一半.

With the answer of nickfox I was able to solve half of my question.

    et.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        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().matches(""))
            {
                et.setText("0.00");
                Selection.setSelection(et.getText(), 0, 4);
            } 
        }
    });

我仍在為我的問題的另一半尋找解決方案.如果我找到了解決方案,我也會在這里發(fā)布.

I'm still working on a solution for the other half of my question. If I found the solution I will post it here too.

OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus){
            String userInput = et.getText().toString();

            int dotPos = -1;    

            for (int i = 0; i < userInput.length(); i++) {
                char c = userInput.charAt(i);
                if (c == '.') {
                    dotPos = i;
                }
            }

            if (dotPos == -1){
                et.setText(userInput + ".00");
            } else {
                if ( userInput.length() - dotPos == 1 ) {
                    et.setText(userInput + "00");
                } else if ( userInput.length() - dotPos == 2 ) {
                    et.setText(userInput + "0");
                }
            }
        }
    }

推薦答案

這是我用來輸入美元的東西.它確保始終只有小數(shù)點后 2 位.您應(yīng)該能夠通過刪除 $ 符號來適應(yīng)您的需要.

Here is something I use to for dollar input. It makes sure that there are only 2 places past the decimal point at all times. You should be able to adapt it to your needs by removing the $ sign.

    amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
    amountEditText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        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().matches("^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"))
            {
                String userInput= ""+s.toString().replaceAll("[^\d]", "");
                StringBuilder cashAmountBuilder = new StringBuilder(userInput);

                while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                    cashAmountBuilder.deleteCharAt(0);
                }
                while (cashAmountBuilder.length() < 3) {
                    cashAmountBuilder.insert(0, '0');
                }
                cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
                cashAmountBuilder.insert(0, '$');

                amountEditText.setText(cashAmountBuilder.toString());
                // keeps the cursor always to the right
                Selection.setSelection(amountEditText.getText(), cashAmountBuilder.toString().length());

            }

        }
    });

這篇關(guān)于EditText 始終顯示帶 2 位小數(shù)的數(shù)字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
Autosizing EditText(自動調(diào)整 EditText)
主站蜘蛛池模板: 中文毛片 | 免费国产视频 | 做爰xxxⅹ性生交 | 精品亚洲国产成人av制服丝袜 | 四虎在线观看 | 三级免费观看 | 在线观看av片 | av在线视屏 | 看毛片的网站 | 91精品久久久久 | 久久艳片www.17c.com | 亚洲成人av在线播放 | 欧美在线观看视频 | 黄色一级片免费 | 精品理论片 | 久久亚洲国产精品 | 亚洲精品色图 | 亚洲成人日韩 | 一级做a爱片性色毛片 | 久久av片 | 亚洲天堂一区 | 深夜福利网 | 久久免费视频观看 | 欧美黑人一区二区三区 | 看逼网站| 精品一区二区三区四区五区 | 一区二区三区在线免费 | yy6080午夜| 国产中文在线 | 国产三级在线播放 | 亚洲日本中文字幕 | 亚洲在线免费视频 | 国产精品久久久久久久久免费桃花 | 一区二区三区中文字幕 | 精品一区二区免费视频 | 久久欧美| 成人免费黄色片 | 天天撸夜夜操 | 久草青青草 | 国产黄在线 | 狠狠草视频 |