安卓:有什么好的解决方案,如何验证编辑框[英] Android: are there any good solutions how to validate Editboxes

本文是小编为大家收集整理的关于安卓:有什么好的解决方案,如何验证编辑框的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

例如,我希望验证最小长度,如果它是Email,a PhoneNumber.

如何在Android中实现这一目标.我想验证.

推荐答案

  1. 如果您需要键入用户键入某些内容,则扩展InputFilter并将其注册为EditText.

    // built in InputFilter.LengthFilter limits the umber of chars
    EditText.setFilters(new Filter[]{new InputFilter.LengthFilter(100)})
    

其他推荐答案

您可以执行许多验证

  1. 添加输入过滤器.更多信息在这里 http://developer.android.com/reference/android/text/inputfilter.html 如何将过滤器添加到可编辑视图 http://developer.android.com/reference/android/text/edable.html#setfilters%28Android.Text.InputFilter

  2. 使用textwatchers在Go上修改内容. More On Textwatchers在这里 http://developer.android.com/reference/android/text/textwatcher.html 使用 http://developer.android.com/reference/android/widget/textview.html#addtextChangedListener(android.text.textwatcher)

注意:其中很少有在Android本身中实现.如果可以使用它们.查找文档中的子类文档和InputFilter

其他推荐答案

用于验证文本框

1.最小长度: U可以直接给出该文本的长度. 2.邮件验证:

public boolean isEmailValid(String email)
        {
             String regExpn =
                 "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                     +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                       +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                       +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                       +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                       +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

         CharSequence inputStr = email;

         pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
         matcher = pattern.matcher(inputStr);

         if(matcher.matches())
            return true;
         else
            return false;
 }

1.对于电话号码:如果要修复长度,则给出长度,输入类型为数字.

本文地址:https://www.itbaoku.cn/post/102471.html

问题描述

For example, I want to Validate Minimum-Length, and if it's Email, a PhoneNumber.

How is this possible in android. I want to validate.

推荐答案

  1. If you wan to prevent user to type something, then extend the InputFilter and register it with your EditText.

    // built in InputFilter.LengthFilter limits the umber of chars
    EditText.setFilters(new Filter[]{new InputFilter.LengthFilter(100)})
    

其他推荐答案

There are a number of things that you can do to validate

  1. Add input filters. More on it is here http://developer.android.com/reference/android/text/InputFilter.html How to add filter to editable view is mentioned here http://developer.android.com/reference/android/text/Editable.html#setFilters%28android.text.InputFilter

  2. Use TextWatchers to modify the content on the go. More on TextWatchers is here http://developer.android.com/reference/android/text/TextWatcher.html Set this up for your EditText using http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)

Note: There are few of them implemented in Android itself. Make use of them if you can. Look for subclasses in the documentation for TextWatcher and InputFilter

其他推荐答案

For the validation of Text box

1. minimum length: u can directly give the length of that text. 2. mail validation:

public boolean isEmailValid(String email)
        {
             String regExpn =
                 "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                     +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                       +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                       +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                       +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                       +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

         CharSequence inputStr = email;

         pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
         matcher = pattern.matcher(inputStr);

         if(matcher.matches())
            return true;
         else
            return false;
 }

1. For phone number: If you want to fix length then give the length and the input type is number.