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

EditText 始終顯示帶 2 位小數的數字

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

問題描述

限時送ChatGPT賬號..

我想始終以兩位小數顯示 EditText 字段的輸入.因此,當用戶輸入 5 時將顯示 5.00,或者當用戶輸入 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.

我已經將輸入類型設置為:

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

android:inputType="number|numberDecimal"/>

我應該在這里使用輸入過濾器嗎?

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

推薦答案

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

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

            }

        }
    });

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

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

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
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(自動調整 EditText)
主站蜘蛛池模板: 在线色网站 | 男人av在线播放 | 国产视频黄色 | 国产欧美在线观看 | 91精品久久久久久久久久 | 四虎影院在线免费观看 | 精品久久久久久久 | 日韩av成人在线 | 97超碰在线免费 | 完全免费av在线 | 高清亚洲| 色资源在线 | 国产精品久久久久一区二区三区 | 91精品久久久 | 午夜影院在线免费观看视频 | 综合久久一区 | 国产一区在线免费观看视频 | 久久亚洲一区二区 | 中文字幕福利视频 | 欧美一级二级三级视频 | 欧美一区不卡 | 日本一道本视频 | 99精品一区二区三区 | 久久精品国产免费看久久精品 | 福利精品 | 成人欧美 | 国产色爽 | 国产精品不卡 | 国产2区 | 久久美国 | 理论片87福利理论电影 | 国产黄色av电影 | 日韩三级一区 | 九九热免费在线观看 | 91精品久久久久久久久 | 亚洲精品片 | 久久久亚洲一区 | 99爱视频| 亚洲永久入口 | 国产探花在线观看视频 | 欧美乱淫视频 |