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

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

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)
主站蜘蛛池模板: 西西裸体做爰视频 | 丁香久久 | 国产不卡在线播放 | 欧美八区| 久久综合影院 | 日韩精品在线看 | 国产偷自视频区视频 | 91在线免费视频 | 国产美女久久久 | 欧美一区二区三区,视频 | 国产成人综合一区二区三区 | 亚洲视频二区 | 夜夜操天天操 | 成人亚洲视频 | 一区二区三区四区在线 | 人人种亚洲 | 欧美aⅴ| 中文字幕亚洲一区二区三区 | sese视频在线观看 | 男人天堂久久久 | 欧美久久一区二区三区 | 成人免费网站视频 | 欧美精品一区二区三区视频 | 欧美激情区 | 人人操日日干 | 日韩在线视频一区 | 日本精品久久 | 亚洲精品一区中文字幕 | 97在线播放 | 一区视频在线 | 久久精品屋 | av大片在线 | www亚洲精品 | 暖暖成人免费视频 | 国产欧美在线播放 | 一级毛片免费视频观看 | 国产午夜精品视频 | 欧美成人a∨高清免费观看 色999日韩 | 欧美日韩国产一区二区三区 | 午夜免费视频 | 一级欧美黄色片 |