4

私は、editTextあなたが何かを書いて、次にクリックすると..あなたが書いたテキストが別のeditTextに書き込まれます..それは完全に機能しています..しかし、私はいくつかの文字を置き換えるためにtextWatcherを使いたい..

例: S を $ に、O を @ にするにはどうすればよいですか?

アップデート:

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

私のコードは、あなたがEditTextに書いたものを下のLarge TextViewに入力します..ボタンを押すTransと、あなたが書いたテキストが設定されますが、いくつかの文字例Sを$に置き換えたい.. Sを入力しても何も起こりません.. Sだけでは$にはなりません..

私は何を間違っていますか?

4

2 に答える 2

4

TextwatcherをeditTextに割り当ててから、次のようにします。

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

これがコードが適用されたtextWatcherです

    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はすぐには起動しないようです。

したがって、文字列を変数に入れる場合は、すべての変更を適用します。次に、テキストボックスに戻します。正常に機能します。(I have tested and working code here Pasted above

于 2012-08-22T21:58:00.663 に答える
1

最初の EditText の値を 2 番目の EditText の値に入れる場合、文字列に対して Java のreplace()関数を使用できます。たとえば、次のコードは "$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);
于 2012-08-22T22:01:22.910 に答える