I do not understand how best to do the implementation of the task in InputFilter ?

There is an EditText field. It is necessary to enter the time in the 24-hour format.

I need to make the colon between the numbers of hours and minutes always always stand , like this:

"__:__" -> "1_:__" -> "12:__" -> "12:3_" -> "12:34"

And the removal to go in this order:

"12:34" -> "12:3_" -> "12:__" -> "1_:__" -> "__:__"

Now I enter these numbers and substitute ":" myself.

I found solutions here, like checking in TextWatcher add a colon programmatically, upon reaching the line length = 2, but I don’t need it. It is necessary that the colon always stands in EditText and when entering the first 2 numbers, the pointer jumped over this colon and I could add the remaining 2 numbers. And you could also delete numbers without deleting the colon.

As I understand it, you need to initially write two spaces in the EditText , a colon and two more spaces so that the colon is in the center (the text is aligned inside the input field), then somehow enter and replace the space with a number when entering text.

Now I have an InputFilter implementation with input numbers from "00:00" to "29:59":

 timeFilter = new InputFilter() { // фильтр ввода. позволяет вводить от 00:00 до 23:59 @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (source.length() > 1 && !doneOnce) { source = source.subSequence(source.length() - 1, source.length()); if (source.charAt(0) >= '0' && source.charAt(0) <= '2') { // первый символ может быть 0,1,2 doneOnce = true; //флаг первого символа в editText return source; } else { return ""; //остальные символы будут зануляться } } if (source.length() == 0) { return null;// deleting, keep original editing } String result = ""; result += dest.toString().substring(0, dstart); result += source.toString().substring(start, end); result += dest.toString().substring(dend, dest.length()); if (result.length() > 5) { // если в поле 5 символов( 1 2 : 4 5 ) return "";// не разрешать добавлять символы } boolean allowEdit = true; char c; if (result.length() > 0) { c = result.charAt(0); allowEdit &= (c >= '0' && c <= '2'); } if (result.length() > 1) { c = result.charAt(1); if (result.charAt(0) == '0' || result.charAt(0) == '1') allowEdit &= (c >= '0' && c <= '9'); else allowEdit &= (c >= '0' && c <= '3'); } if (result.length() > 2) { c = result.charAt(2); allowEdit &= (c == ':'); } if (result.length() > 3) { c = result.charAt(3); allowEdit &= (c >= '0' && c <= '5'); } if (result.length() > 4) { c = result.charAt(4); allowEdit &= (c >= '0' && c <= '9'); } return allowEdit ? null : ""; } }; 

    1 answer 1

    Drawing a layout from EditText + TextView + EditText will be quite working option.

    • In the middle will be your colon.
    • The input listeners in EditText will check the length of the entered / erased one and throw the focus onto another EditText
    • and then I have to make 2 different filters for the fields? - zayn1991
    • @ zayn1991, yes, but it will certainly be easier. In addition, it seems, it is possible to prescribe in the properties of the input field that it can be entered and what is not. Type only tsmfry. It will be easier already) - YuriySPb
    • one
      Okay, I will give up with my idea one editText and I will do, as you suggested) - zayn1991