Android的使用TextWatcher代替字词 [英] Android using TextWatcher to replace words
问题描述
我有一个 EDITTEXT
,您在写东西,然后当u点击下一步..ü写的文字被写入到另一个EDITTEXT ..
它的工作完美地..
但是,我想用textWatcher替换一些字母。
I have an editText
, that you write in it something then when u click next .. the text u wrote gets written into another editText ..
It's working perfectly ..
But I want to use textWatcher to replace some letters..
例子:我如何在S为$
否则O为@
Example: How do I make the S to be $ or the O to be @
更新:
final EditText First = (EditText)findViewById(R.id.etFirst);
String strFirst = First.getText().toString();
final TextView done = (TextView)findViewById(R.id.tvName);
String strDone = done.getText().toString();
Button Trans = (Button) findViewById(R.id.bTrans);
Trans.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//First EditText
if (First.getText().toString().equals("John")) {
done.setText("JOHN");
} else {
if (First.getText().toString().equals("Ahmed")) {
done.setText("AHMED");
} else {
done.setText(First.getText());
}
}
};
});
First.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
done.setText(First.getText().toString().replaceAll("S", "$"));
}
});
我的code将键入的内容,你在写的EditText一个大的TextView下面..当我preSS按钮跨
它设置其文本你写的,但我想,以取代一些字母例如S键$ ..当我输入没什么发生..只是原来是这样做不是得到为$ ..
my code will type what you wrote in the EditText to a Large TextView below .. when I press the button Trans
it sets the text which you wrote but I want to replace some letters example S to $ .. when I type S nothing happens .. just S it don't gets to be $ ..
我在做什么错了?
推荐答案
分配Textwatcher您EDITTEXT然后像做
Assign the Textwatcher to your editText then do something like
editText.setText(editText.getText().toString().replaceAll("S", "$"));
下面是应用了code中的textWatcher
Here is the textWatcher with code applied
final EditText First = (EditText)findViewById(R.id.etFirst);
String strFirst = First.getText().toString();
final TextView done = (TextView)findViewById(R.id.tvName);
String strDone = done.getText().toString();
Button Trans = (Button) findViewById(R.id.bTrans);
Trans.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//First EditText
if (First.getText().toString().equals("John")) {
done.setText("JOHN");
} else {
if (First.getText().toString().equals("Ahmed")) {
done.setText("AHMED");
} else {
done.setText(First.getText());
}
}
};
});
First.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
String text = First.getText().toString();
text = text.replace('s', 'd'); // Any of the diffrent replace methods should work.
text = text.replace('S', '$'); // This uses the char replace method. Note, the ' quotations
text = text.replace('O', '@');
text = text.replace('o', '@');
done.setText(text);
}
});
其可能在事实居住的错误,你将再次得到老的字符串,并应用 text.replace
就可以了。而不是修改后的字符串。我本来期望它已经得到了新的字符串,但的setText似乎没有立即开火。
Its possible that the error resided in the fact you are getting the old string again, and applying text.replace
on it. Rather than the modified string. I would have expected it to have got the new string, but setText doesnt seem to fire immediately.
所以,如果你得到的字符串到一个变量,然后应用所有更改。然后把它回到文本框它将很好地工作。 (我已经测试和工作code上面粘贴在这里
)
So if you get the string into a variable, then apply all your changes. Then put it back to your text box it will work fine. (I have tested and working code here Pasted above
)
这篇关于Android的使用TextWatcher代替字词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!