Assign Textwatcher to your editText, then do something like
editText.setText(editText.getText().toString().replaceAll("S", "$"));
Here is a text editor with the code entered
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); } });
Perhaps the error arose because you get the old line again and apply text.replace to it. Instead of a modified line. I would expect it to get a new line, but setText doesn't seem to work immediately.
So, if you enter a string into a variable, apply all your changes. Then return it to the text box, it will work fine. ( I have tested and working code here Pasted above )
source share