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

如何屏蔽 EditText 以顯示 dd/mm/yyyy 日期格式

How to mask an EditText to show the dd/mm/yyyy date format(如何屏蔽 EditText 以顯示 dd/mm/yyyy 日期格式)
本文介紹了如何屏蔽 EditText 以顯示 dd/mm/yyyy 日期格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何格式化 EditText 以遵循dd/mm/yyyy"格式,就像我們可以使用 TextWatcher 掩蓋用戶輸入看起來像0.05€".我不是在談論限制字符或驗證日期,只是屏蔽為以前的格式.

How can I format an EditText to follow the "dd/mm/yyyy" format the same way that we can format using a TextWatcher to mask the user input to look like "0.05€". I'm not talking about limiting the characters, or validating a date, just masking to the previous format.

推薦答案

我為一個項目編寫了這個 TextWatcher,希望對某人有所幫助.請注意,它驗證用戶輸入的日期,您應該在焦點更改時處理它,因為用戶可能還沒有完成輸入日期.

I wrote this TextWatcher for a project, hopefully it will be helpful to someone. Note that it does not validate the date entered by the user, and you should handle that when the focus changes, since the user may not have finished entering the date.

25/06 更新 將其設為 wiki,看看我們是否能找到更好的最終代碼.

Update 25/06 Made it a wiki to see if we reach a better final code.

07/06 更新我終于為觀察者本身添加了某種驗證.它將對無效日期執行以下操作:

Update 07/06 I finally added some sort of validation to the watcher itself. It will do the following with invalid dates:

  • 如果月份大于 12,則為 12(十二月)
  • 如果日期大于所選月份的日期,請將其設為該月份的最大值.
  • 如果年份不在1900-2100范圍內,請將其更改為在范圍內
  • If the month is greater than 12, it will be 12 (December)
  • If the date is greater than the one for the month selected, make it the max for that month.
  • If the year is not in the range 1900-2100, change it to be in the range

這個驗證符合我的需要,但是你們中的一些人可能想稍微改變一下,范圍很容易改變,你可以把這個驗證掛到 Toast 消息,例如,通知用戶我們已經修改了他/她的日期,因為它是無效的.

This validation fits my needs, but some of you may want to change it a little bit, ranges are easily changeable and you could hook this validations to Toast message for instance, to notify the user that we've modified his/her date since it was invalid.

在這段代碼中,我假設我們有一個名為 dateEditText 引用,它附有這個 TextWatcher,可以這樣做:

In this code, I will be assuming that we have a reference to our EditText called date that has this TextWatcher attached to it, this can be done something like this:

EditText date;
date = (EditText)findViewById(R.id.whichdate);
date.addTextChangedListener(tw);

<小時>

TextWatcher tw = new TextWatcher() {
    private String current = "";
    private String ddmmyyyy = "DDMMYYYY";
    private Calendar cal = Calendar.getInstance();

當用戶更改 EditText

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!s.toString().equals(current)) {
            String clean = s.toString().replaceAll("[^\d.]|\.", "");
            String cleanC = current.replaceAll("[^\d.]|\.", "");

            int cl = clean.length();
            int sel = cl;
            for (int i = 2; i <= cl && i < 6; i += 2) {
                sel++;
            }
            //Fix for pressing delete next to a forward slash
            if (clean.equals(cleanC)) sel--;

            if (clean.length() < 8){
               clean = clean + ddmmyyyy.substring(clean.length());
            }else{
               //This part makes sure that when we finish entering numbers
               //the date is correct, fixing it otherwise
               int day  = Integer.parseInt(clean.substring(0,2));
               int mon  = Integer.parseInt(clean.substring(2,4));
               int year = Integer.parseInt(clean.substring(4,8));

               mon = mon < 1 ? 1 : mon > 12 ? 12 : mon;
               cal.set(Calendar.MONTH, mon-1);
               year = (year<1900)?1900:(year>2100)?2100:year;
               cal.set(Calendar.YEAR, year); 
               // ^ first set year for the line below to work correctly
               //with leap years - otherwise, date e.g. 29/02/2012
               //would be automatically corrected to 28/02/2012 

               day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day;
               clean = String.format("%02d%02d%02d",day, mon, year);
            }

            clean = String.format("%s/%s/%s", clean.substring(0, 2),
                clean.substring(2, 4),
                clean.substring(4, 8));

            sel = sel < 0 ? 0 : sel;
            current = clean;
            date.setText(current);
            date.setSelection(sel < current.length() ? sel : current.length());
        }
    }

我們還實現了其他兩個功能,因為我們必須這樣做

We also implement the other two functions because we have to

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

    @Override
    public void afterTextChanged(Editable s) {}
};

這會產生以下效果,刪除或插入字符將顯示或隱藏 dd/mm/yyyy 掩碼.它應該很容易修改以適應其他格式掩碼,因為我試圖讓代碼盡可能簡單.

This produces the following effect, where deleting or inserting characters will reveal or hide the dd/mm/yyyy mask. It should be easy to modify to fit other format masks since I tried to leave the code as simple as possible.

這篇關于如何屏蔽 EditText 以顯示 dd/mm/yyyy 日期格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 9久久婷婷国产综合精品性色 | 久久久久国产一区二区三区 | 久久久青草婷婷精品综合日韩 | 中文二区 | 拍戏被cao翻了h承欢 | 亚洲综合视频 | 久久精品免费一区二区三 | 农夫在线精品视频免费观看 | 99pao成人国产永久免费视频 | 91资源在线 | 欧美一级高潮片免费的 | 狠狠热视频 | 黄色精品 | 亚洲精品免费视频 | 日韩一区二区三区av | 久久久国产一区二区三区四区小说 | 欧美区日韩区 | 亚洲不卡在线观看 | 日韩视频精品在线 | 成人毛片在线视频 | 91精品国产91久久久久久吃药 | 一级片av | 亚洲一区二区三区免费观看 | 国产精品中文字幕在线 | 热re99久久精品国产99热 | 欧美国产精品久久久 | 亚洲精品久久久久中文字幕欢迎你 | 国产精品久久久久久238 | 精品九九 | 中文字幕免费视频 | 欧美日韩国产精品 | 色综合区| 欧美日韩久久 | 久久亚洲国产精品日日av夜夜 | 欧美国产日韩一区 | 91在线电影| 亚洲视频三区 | 欧美激情精品久久久久久 | 欧美一区二区在线观看 | 婷婷五月色综合 | 亚洲午夜在线 |