我有edittext
。我想在使用时输入一个句子。在一个空格之后,一些单词将变成另一个单词。我想仅使用addTextChangedListener
执行此操作。
我使用以下代码:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable str) {
Editable ab = new SpannableStringBuilder(str.toString().replace("A", "a"));
editText.setText(ab);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
答案 1 :(得分:0)
你可以写一个像这样的方法。首先检查用户是否输入了空格。然后将这个词从空格中分开并替换你的单词。
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.subSequence(s.length()-1, s.length()).length()==0)
{
replaceWord(s.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
private void replaceWord(String word)
{
String wordsList[] = word.split(" ");
//Replace words here
}