Android using TextWatcher to replace words

本小妞迷上赌 提交于 2019-12-12 12:15:32

问题


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..

Example: How do I make the S to be $ or the 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 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 $ ..

What am I doing wrong ?


回答1:


Assign the Textwatcher to your editText then do something like

editText.setText(editText.getText().toString().replaceAll("S", "$"));

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);
            }
        });

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.

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)




回答2:


When putting in the value of your first EditText into the value of the second EditText, you can use Java's replace() function for 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);


来源:https://stackoverflow.com/questions/12081924/android-using-textwatcher-to-replace-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!