Android 在输入一个字符串后防止字符串中出现空白

Android 在输入一个字符串后防止字符串中出现空白,android,string,substring,whitespace,Android,String,Substring,Whitespace,我编写了一个用户不能输入字符串中第一个空格的代码。 允许用户在至少2个字符后输入空格。 我需要重新定义我的方法,以便用户只在两个或更多字符后输入一次空白。在那之后,应该加以预防。我该怎么做 案例更新\u名称: if(firstName.getText().toString().startsWith(“”) firstName.setText(firstName.getText().toString().trim()); if(firstName.getText().toString()包含(“

我编写了一个用户不能输入字符串中第一个空格的代码。 允许用户在至少2个字符后输入空格。 我需要重新定义我的方法,以便用户只在两个或更多字符后输入一次空白。在那之后,应该加以预防。我该怎么做

案例更新\u名称:
if(firstName.getText().toString().startsWith(“”)
firstName.setText(firstName.getText().toString().trim());
if(firstName.getText().toString()包含(“”)
firstName.setText(firstName.getText().toString().replace(“,”);
int indexOfSpace=firstName.getText().toString().lastIndexOf(“”);
如果(indexOfSpace>0){
String beforeSpace=firstName.getText().toString().substring(0,indexOfSpace);
String[]splitted=beforeSpace.split(“”);
如果(拆分!=null&&splitted.length>0){
if(已拆分[splited.length-1].length()<2)
firstName.setText(firstName.getText().toString().trim());
}
}
使用正则表达式。我认为这应该符合你的要求

\S{2}\S*\s\S*\n

Explanation:
\S{2} two non whitespace
\S* n non whitespace
\s a whitespace
\S* n non whitespace
\n newline (i only added that for regexr, you may not need it)
另一种方式:
迭代
String.charAt(int)
,如果前两个字符中有空格,则返回false,计算所有空格,如果n>1,则返回false。

您需要做的是使用TextWatcher

public class CustomWatcher implements TextWatcher {

private String myText;
private int count = 0;

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
    myText= s;
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
    //check if there is a space in the first 2 characters, if so, sets the string to the previous before the space
    if(s.length() < 3 && s.contains(" "))
        s= myText;

    //if the length is higher than 2, and the count is higher than 0 (1 space added already), puts the string back if a space is entered
    else if(s.contains(" ") && count > 0)
        s= myText;

    //If none of the above is verified and you enter a space, increase count so the previous if statement can do its job
    else if(s.contains(" "))
        count++;

}

您可以使用控制editText(我假设),如果长度则只需检查内部的PostTextChanged()。此方法应满足您的要求:

private static boolean isValidFirstName(String firstName) {
    if (firstName != null && !firstName.startsWith(" ")) {
        int numberOfSpaces = firstName.length() - firstName.replace(" ", "").length();
        if (firstName.length() < 2 || numberOfSpaces <= 1) {
            return true;
        }
    }
    return false;
}
private静态布尔值isValidFirstName(字符串名){
if(firstName!=null&&!firstName.startsWith(“”){
int numberOfSpaces=firstName.length()-firstName.replace(“,”).length();

如果(firstName.length()<2 | | numberOfSpaces)如何将字母添加到此模式中\S实际上是字母/数字等。它包含除空格以外的所有字符,
private static boolean isValidFirstName(String firstName) {
    if (firstName != null && !firstName.startsWith(" ")) {
        int numberOfSpaces = firstName.length() - firstName.replace(" ", "").length();
        if (firstName.length() < 2 || numberOfSpaces <= 1) {
            return true;
        }
    }
    return false;
}