这是我尝试做的事情。我需要在数字输入和字母输入之间添加一个空格。我正在使用TextWatcher类,但我无法得到我想要的内容
如果输入文本是:
APyt04XC2446
我需要它
APyt 04 XC 2446
但我得到了这个:
APyt 0 4 X C 2 4 4 6
这是我的TextWatcher类:
public class FormattingTextWatcher implements TextWatcher {
private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String source = s.toString();
if (!lastSource.equals(source)) {
source = source.replace(WHITE_SPACE, EMPTY_STRING);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < source.length(); i++) {
if ((s.charAt(i)>='a' && s.charAt(i)<='z') || (s.charAt(i)>='A' && s.charAt(i)<='Z')) {
} else {
stringBuilder.append(WHITE_SPACE);
}
stringBuilder.append(source.charAt(i));
}
lastSource = stringBuilder.toString();
s.replace(0, s.length(), lastSource);
}
} }
请让我离开这个...谢谢
答案 0 :(得分:0)
试试这个:
public class FormattingTextWatcher implements TextWatcher {
private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String source = s.toString();
int letterFlag = -1;
if(Character.isLetter(character.charAt(0)){
letterFlag = 0;
}
else{
letterFlag = 1;
}
for (int i = 1; i < source.length(); i++) {
if(letterFlag == 1){
if(Character.isLetter(character.charAt(i)){
stringBuilder.append(WHITE_SPACE);
letterFlag = 0;
}
}
else{
if(Character.isDigit(character.charAt(i)){
stringBuilder.append(WHITE_SPACE);
letterFlag = 1;
}
}
stringBuilder.append(source.charAt(i));
}
} }
答案 1 :(得分:0)
为你
public class FormattingTextWatcher implements TextWatcher {
private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String source = s.toString();
if (!lastSource.equals(source)) {
source = source.replace(WHITE_SPACE, EMPTY_STRING);
StringBuilder stringBuilder = new StringBuilder();
if ((str.charAt(0) >= 'a' && str.charAt(0) <= 'z') || (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z'))
flag = 1;
else if ((str.charAt(0) >= '0' && str.charAt(0) <= '9'))
flag = 0;
for (int i = 0; i < str.length(); i++) {
if (((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) && flag == 0) {
stringBuilder.append(WHITE_SPACE);
stringBuilder.append(str.charAt(i));
flag = 1;
continue;
} else if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') && flag == 1) {
stringBuilder.append(WHITE_SPACE);
stringBuilder.append(str.charAt(i));
flag = 0;
continue;
}
stringBuilder.append(str.charAt(i));
}
lastSource = stringBuilder.toString();
lastSource = lastSource.replaceAll("\\s+", " "); // it would remove the extra spaces between characters
s.replace(0, s.length(), lastSource);
}
}
}