Android EditText中名称的验证

Android EditText中名称的验证,android,android-edittext,Android,Android Edittext,在我的应用程序中,我有字段名。如果字段中包含数字或.,?,则它工作正常。,?。。。但当我输入usman(空格)shafi时,它给出了错误。我的语句在两个单词之间使用空格作为无效字符。谁能告诉我怎么做。谢谢 这是密码 if(!Pattern.matches("[a-zA-Z]+", textname.getText().toString().trim())){ textname.setError("Invalid Characters"); } //

在我的应用程序中,我有字段名。如果字段中包含数字或.,?,则它工作正常。,?。。。但当我输入usman(空格)shafi时,它给出了错误。我的语句在两个单词之间使用空格作为无效字符。谁能告诉我怎么做。谢谢

这是密码

   if(!Pattern.matches("[a-zA-Z]+", textname.getText().toString().trim())){

         textname.setError("Invalid Characters");

     }
    // if(!Pattern.matches("[a-zA-Z]+", textkin.getText().toString().trim())){

  //     textkin.setError("Invalid Characters");

    // }");

将空间添加到常规表达式:

    if(!Pattern.matches("[a-zA-Z ]+", textname.getText().toString().trim())){

         textname.setError("Invalid Characters");

     }

如果里面只有一个空白,您可以尝试以下方法:

text = textname.getText().toString().trim();
string[] txtSplt = text.split(" ");

text = "";
foreach (string s : txtSplt)
{
    text += s;
}

然后,您可以在不更改正则表达式的情况下继续检查条件。

这样做。你的问题会解决的

    String regex = "([A-Z a-z]+)";
    Pattern pattern1 = Pattern.compile(regex);
    Matcher matcher1 = pattern1.matcher(textname.getText().toString());

    if(!matcher1.matches()){
          textname.setError("Invalid Characters");
    }
你能试试这个“^/s^[a-zA-Z]+$/s”吗