我必须允许用户即时以##:##格式在编辑文本中输入时间,有什么方法可以实现?我用下面的代码,但它不起作用.
我可以输入大于24的值,例如45623:5689.
edit.setinputType(inputType.TYPE_DATETIME_VARIATION_TIME)
甚至androID:text =“ time”也不起作用.
我怎么能做到这一点.有人可以建议我该怎么做.
我想允许用户在前2个地方输入最多23个值,然后进行计算:然后用户最多可以输入59个值.
例如
23:59 correct24:05 incorrect02:56 correct02:79 incorrect
我也使用了这个自定义过滤器,但是它不起作用
我是从SO的其他地方获得此代码的.
码:
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 "";// do not allow this 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 : ""; } };
编辑问题:main.xml文件代码
<?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>
此代码有效,但是如果我插入第一个字母并插入适当的值,则它不起作用,因为源包含其先前的字符值.
解决方法:
尝试将字符转换为整数,然后测试它们是否大于24和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.}
减48,因为数字0在ascii表中排在第48位.测试必须是ascii.
总结以上是内存溢出为你收集整理的java-如何限制android中edittext的输入时间全部内容,希望文章能够帮你解决java-如何限制android中edittext的输入时间所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)