Android using TextWatcher to replace words

I have editText that you write something in it when u press next .. the text that u wrote is written to another editText .. It works fine. But I want to use textWatcher to replace some letters.

Example: How to make S equal to $ or O to be @

UPDATE:

  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", "$")); } }); 

my code will print what you wrote in EditText, in Large TextView below .. when I click the Trans button, it sets the text you wrote, but I want to replace some letters of example S with $ .. when I type S nothing going on .. just s it won't be $ ..

What am I doing wrong?

+4
source share
2 answers

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 )

+4
source

When transferring the value of your first EditText to the value of the second EditText, you can use the Java replace() function for the strings. For example, the following code prints "$ X @ $$".

 String value = "SXOSS"; String newValue = value.replace("S", "$"); //replaces all instances of S with $ newValue = newValue.replace("O", "@"); //replaces all instances of O with @ System.out.println(newValue); 
+1
source

All Articles