【问题标题】:How to validate an EditText when i enter Special charecters输入特殊字符时如何验证 EditText
【发布时间】:2015-05-04 11:08:10
【问题描述】:

我有三个编辑文本。 第一个没有特殊字符的 EditText 名称。当我输入任何特殊字符时。我想显示一个错误,不要输入任何特殊字符。 当我离开第二个编辑文本时,我的光标在第三个编辑文本中。我想显示一个红色边框编辑文本。

【问题讨论】:

  • 你只想要文本还是字母数字?
  • 嗨 Vishalk,当我输入任何特殊字符时,我只想输入文本。我想显示红色边框编辑文本
  • 请参考stackoverflow.com/questions/16160989/… 可能对你有帮助。

标签: android


【解决方案1】:
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

在 xml 文件的 edittext 中使用上述行,以便它只输入字母和数字

或

在edittext中添加textwatcher

yourEditText.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {

           // Here you need to check special character, if found then show error message

          if (s.toString().contains("%"))
          {
               // Display error message
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });

【讨论】:

    【解决方案2】:

    这是一个示例,展示如何验证用户输入的 EditText 并相应地更新视图边框。

    首先实现 TextWater 并将其附加到您的 EditView,如下所示:

    活动代码:

    public class MainActivity extends ActionBarActivity {
    
    EditText editText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        editText = (EditText) findViewById(R.id.edittext);
    
        // Attach TextWatcher
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
                // A-Z, a-z, space
                String pattern = "^([a-zA-Z ]*)$";
    
                // Check for pattern match
                if (!editable.toString().matches(pattern)) {
    
                    // Set red border 
                    editText.setBackgroundResource(R.drawable.red_border);
                } else {
    
                    // Restore default border
                    editText.setBackgroundResource(R.drawable.grey_border);
                }
            }
        });
    }
    }
    

    用于在 EditText 周围绘制边框的两个可绘制文件

    /res/drawable/grey_border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        android:shape="rectangle"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#FFFFFF"/>
        <stroke
            android:width="2dp"
            android:color="#000000"/>
    </shape>
    

    /res/drawable/grey_border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        android:shape="rectangle"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#FFFFFF"/>
        <stroke
            android:width="2dp"
            android:color="#ff0000"/>
    </shape>
    

    最后是布局文件:

    /res/layout/activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
    <EditText
        android:padding="10dp"
        android:layout_margin="10dp"
        android:background="@drawable/grey_border"
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      将 TextWatcher 添加到您的 EditText 并应用识别特殊字符的条件(输入时),然后您可以在条件中设置它

      editText.setError("You cannot use Special Characters");
      

      【讨论】:

        【解决方案4】:

        您应该使用正则表达式来检查输入。这个link 提供文档并描述您可能使用的模式。

        对于您的情况,如果您只想使用文本字符:

        public boolean isValid(String editTextInput){
             boolean isInputValid = false;
        
             String expression = "^[a-zA-Z]";
             CharSequence inputStr = editTextInput;
        
             Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
             Matcher matcher = pattern.matcher(inputStr);
             if (matcher.matches()) {
                  isInputValid = true;
             }
             return isInputValid ;
        }
        

        您可以立即使用此方法。当您需要检查输入时,只需放置以下代码:

        if(isValid(editText.getText().toString().trim()){
            //valid
        }else{
            editText.setError("Can contain only text");
            editText.setText("");
        }
        

        如果您想在用户进行新输入时立即检查它,那么您必须实现interface TextWatcher 并将其设置为您的EditText。示例:

        editText.addTextChangedListener(this);
        
        @Override
        public void beforeTextChanged(CharSequence s, int i, int i2, int count){}
        @Override
        public void onTextChanged(CharSequence s, int i, int i2, int count) {}
        @Override
        public void afterTextChanged(Editable editable) {
            if(isValid(editText.getText().toString().trim()){
                //valid
            }else{
                editText.setError("Only alphabet characters allowed");
                editText.setText("");
            }
        }
        

        也可以找到很好的教程here。

        【讨论】:

          猜你喜欢
          • 2015-09-13
          • 1970-01-01
          • 1970-01-01
          • 2013-05-16
          • 2019-04-02
          • 1970-01-01
          • 2011-03-09
          • 2020-01-26
          • 1970-01-01
          相关资源
          最近更新 更多