如果我在输入数据时向编辑文本添加任何特殊符号,我需要删除字符。例如,我输入一个单词 smwinææ 是一个特殊字符,所以如果我添加该字符编辑文本应该删除 æ 并仅显示smwin 通过替换 æ .i 使用文本更改侦听器。检查下面的代码
email.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
System.out.println("started after");
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
System.out.println("started");
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence searchcontact, int start,
int before, int count) {
System.out.println("sssssssssss"+searchcontact);
String a=searchcontact.toString();
System.out.println("casted "+a);
String[] parts = a.split(" ");
String lastWord = parts[parts.length - 1];
System.out.println("------------------------------"+lastWord);
// String lastWord = a.substring(a.lastIndexOf(" ")+1);
// System.out.println("lastword"+lastWord);
if(a.equals("p"))
{
try {
email.getText().delete(email.getSelectionEnd() - 1, email.getSelectionStart());
} catch (Exception e) {
try {
email.getText().delete(email.length() - 1, email.length());
} catch (Exception myException) {
//textfield.getText().delete(textfield.length(), textfield.length() - 1);
}
}
// Method User For Sort Out THE Words
// in
// The SearchBar
}
}
});
在这里,当文本更改时,我尝试获取字符串中的最后一个单词,但我没有这样做。使用
字符串 a=searchcontact.toString(); System.out.println("铸造的"+a); String[] 部分 = a.split(" "); 字符串 lastWord = parts[parts.length - 1];
得到最后一个字,但它在edittext中打印整个字符串我该怎么做,请帮忙