i have allow user input time in ##:## format in edit text on fly, there way achieve it? have used below code doest not working.
i able enter number more 24 value 45623:5689.
edit.setinputtype(inputtype.type_datetime_variation_time)
even android:text="time"
not working.
how can achieve thing. can suggest me how can thing.
i want allow user enter in first 2 places 23 value , compulasary : , user can allow 59 value.
for example
23:59 correct 24:05 incorrect 02:56 correct 02:79 incorrect
i used customize filter not working
i got code else in so.
code:
inputfilter timefilter = new inputfilter() { public charsequence filter(charsequence source, int start, int end, spanned dest, int dstart, int dend) { 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) { return "";// not allow edit } 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); allowedit &= (c >= '0' && c <= '9'); } 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 : ""; } };
edited question : main.xml file code
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="10dp" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:gravity="center" android:orientation="horizontal" > <textview android:id="@+id/txtrecipientname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingright="20dp" android:text="@string/recipient_name" /> <edittext android:id="@+id/edtxtrecipient" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ems="10" android:paddingleft="20dp" > <requestfocus /> </edittext> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:gravity="center" android:orientation="horizontal" > <textview android:id="@+id/txtparceldelivertime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingright="20dp" android:text="@string/delivered_time" /> <edittext android:id="@+id/edtxtparceldelivertime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ems="10" android:paddingleft="20dp" > </edittext> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:gravity="center" android:orientation="horizontal" > <button android:id="@+id/btnrecipient_ok" android:layout_width="100dp" android:layout_height="wrap_content" android:text="@android:string/ok" /> </linearlayout> </linearlayout>
this code working if insert first alphabet , insert proper value not working because source
contains previous character value.
try casting chars ints, test if greater 24 , 60.
int = ((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 this. }
minus 48 because numbers 0 48th in ascii table. the test has ascii.