How to restrict to input time for edittext in android

后端 未结 5 1118
甜味超标
甜味超标 2021-01-17 18:34

I have to allow user to input only time in ##:## format in edit text on the fly, is there any way to achieve it? I have used below code but it doest not working.

I a

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-17 19:24

    //THis handles the input for the date and time at the runtime and all the //invalid atttempt are automatically consumed
    //this is an alternative to regex expression that you can implement

    //using the input regulation for the time data
        timeField.addTextChangedListener(new TextWatcher() {
            String beforeTXT;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    Log.i("before TEXT TEXXT", " this : "+s+" and "+start+" and "+count+"and "+after);
                    beforeTXT= ""+s;
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int input ;
    
                //first determine whether user is at hrs side or min side
                if (s.toString().equals("")){
                    return;
                }
                if(s.toString().length()>2 && start<=2){ //means the user is at hour side
                    input = Integer.parseInt(s.toString().substring(0,1)) % 10;
    
                }
                else if(s.toString().length()>2 && start>=3) {//means that user is at min side
                    input = Integer.parseInt("0"+s.toString().substring(3))%10;
    
                }
                else if(s.toString().indexOf(":")==1){ // if we have for eg 1: or 0: then we take first character for parsing
                    input = Integer.parseInt(s.toString().charAt(0)+"");
                }
                else{ //else it is default where the user is at first position
                    input = Integer.parseInt(s.toString()) % 10;
                }
    
                //Special case where 00: is autommatically converted to 12: in 12hr time format
                if(s.toString().contains("00:")){
                    Log.i("INsisde )))","i am called ");
                    timeField.setText("12:");
                   return;
                }
    
                //Now we manipulate the input and its formattin and cursor movement
                if(input<=1 && start ==0){ //thiis is for first input value to check .... time shouldnt exceed 12 hr
                    //do nothing
                }
                else if (input>1 && start==0){ //if at hour >1 is press then automaticc set the time as 02: or 05: etc
                 timeField.setText("0"+s+":");
                }
                else if(input>2 && start==1 && !s.toString().startsWith("0")){ //whe dont have greater than 12 hrs so second postionn shouldn't exceed value 2
                    timeField.setText(beforeTXT);
                }
                else if(start==1 && !beforeTXT.contains(":")){  //if valid input 10 or 11 or 12 is given then convert it to 10: 11: or 12:
                    timeField.setText(s.toString()+":");
    
                    if(s.toString().length()==1 && s.toString().startsWith("0")){
                    timeField.setText("");
                    }
                    if(s.toString().startsWith("1")&& s.toString().length()==1){ //on back space convert 1: to 01:
                        timeField.setText("0"+timeField.getText().toString());
                    }
    
               }
                else if(start == 3 && input >5 ){ //min fig shouldn't exceed 59 so ...if at first digit of min input >5 then do nothing or codpy the earlier text
                    timeField.setText(beforeTXT);
                }
               else if (start>4 && s.toString().length()>5){ // the total string lenght shouldn't excced 5
                    timeField.setText(beforeTXT);
                }
                else if(start<2 && beforeTXT.length()>2){
                    timeField.setText(beforeTXT);
    
                }
    
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
                Log.i("after  TEXT TEXXT", " this : "+s);
                timeField.setSelection(timeField.getText().toString().length());
    
            }
        });
    

提交回复
热议问题