如何限制android中edittext的输入时间 [英] How to restrict to input time for edittext in android

查看:169
本文介绍了如何限制android中edittext的输入时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须允许用户只在##中输入时间:##格式即时编辑文本,有什么办法可以实现吗?我使用下面的代码,但它不起作用。

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.

我能输入超过24的数字,如45623:5689。

I able to enter number more than 24 value like 45623:5689.

edit.setInputType(InputType.TYPE_DATETIME_VARIATION_TIME)

甚至 android:text =time也无效。

我怎样才能实现这个目标。有人可以建议我怎么做这件事。

how can i achieve this thing. Can anybody suggest me how can i do this thing.

我想允许用户输入最多2个位置,最多23个值,然后compulasary:然后用户可以允许最多59个值。

I want to allow user to enter in first 2 places up to 23 value and then compulasary : and then user can allow up to 59 value.

例如

23:59 correct
24:05 incorrect
02:56 correct
02:79 incorrect

我也使用了这个自定义过滤器,但它不能正常工作

I used this customize filter also but its not working

我从SO中的其他地方得到了这个代码。

I got this code from some where else in 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>

此代码正常工作,但如果我插入第一个字母并插入适当的值,那么它无效,因为 source 包含其先前的字符值。

This code is working but if i insert first alphabet and insert proper value then its not working because source contains its previous character value.

推荐答案

而不是char为什么不使用string,因为char也可以用于比较,因为它可以返回数字

Instead of char why dont you use string, Because char can also be used for comparsion as it can return numbers

char c ='a';
    if(c>10)
    //do something

    //OR
int x = c;

那么为什么不使用String代替char

So why dont you use String instead of char

或者你可以做的是,使用子字符串或类似的东西获取前两个字符并使用Integer.parse()方法来解析它,如果它成功解析然后它是一个有效数字,否则它不是这样你可以验证它并为下两个字符做同样的事情

or what you can do is, take 1st two chars using substring or something like that and use Integer.parse() method to parse it, if it successfully parsed then its a valid number else it is not so you can validate it and similarly do it for next two chars

编辑

EDIT

如果你想像这样实现
23:59更正
24:05不正确
02:56更正
02:79不正确

If you wanted to implement like this 23:59 correct 24:05 incorrect 02:56 correct 02:79 incorrect

然后这是从我这边工作的代码

Then here is the code that worked from my side

public class MainActivity extends Activity {
 InputFilter timeFilter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timeFilter  = new InputFilter() {
        @Override
        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);
                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 : "";
        }

    };

    EditText txt1 = (EditText) findViewById(R.id.edTxtParcelDeliverTime);
    txt1.setFilters(new InputFilter[]{timeFilter});
}
}

我刚把你的XML作为我的主电源放置layout
AND XML没有变化
现在尝试这个并告诉?

I have just taken your XML and placed as my mains layout AND there are no changes to XML Now try this and tell ?

编辑2
现在我在这里为firs char添加了一个使用doneOnce布尔值的验证
现在可以使用,告诉我你现在这个代码是否有任何其他问题

EDIT 2 Now here i have added a validtion for firs char check using doneOnce boolean value This works now, tell me if you have any other problem from this code now

public class MainActivity extends Activity {
EditText edt1;
InputFilter timeFilter;
private String LOG_TAG = "MainActivity";
private boolean doneOnce = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timeFilter  = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                int dstart, int dend) {

            if(source.length() > 1 && doneOnce == false){
                source = source.subSequence(source.length()-1, source.length());
                if(source.charAt(0)  >= '0' && source.charAt(0) <= '2'){
                    doneOnce = true;
                    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) {
                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);
                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 : "";
        }

    };


    edt1 = (EditText) findViewById(R.id.edTxtParcelDeliverTime);
    edt1.setFilters(new InputFilter[] { timeFilter });

}
}

这篇关于如何限制android中edittext的输入时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆