How to restrict to input time for edittext in android

后端 未结 5 1116
甜味超标
甜味超标 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:20

    Try casting the chars to ints, then test if they are greater than 24 and 60.

    int a = ((int) result.charAt(0)) - 48;
    int b = ((int) result.charAt(1)) - 48;
    int c = ((int) result.charAt(3)) - 48;
    if(a < 0 || b < 0 || c < 0) {
        Not right.
    }
    
    if((a > 2 || (a == 2 && b > 3)) || c > 59) {
        Neither is this.
    }
    

    Minus 48 because numbers 0 is 48th in the ascii table. The test has to be ascii.

提交回复
热议问题