当前位置: 首页 > 知识库问答 >
问题:

Android使用TextWatcher替换单词

晏和风
2023-03-14

我有一个editText,当你点击next时,你可以在其中写一些东西。你写的文本会被写入另一个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中写的内容输入到下面的大文本视图中…当我按下按钮Trans时,它会设置你写的文本,但我想将一些字母(例如S)替换为$…当我键入S时,什么都不会发生…只是S不会变成$…

我做错了什么?

共有2个答案

西门奇希
2023-03-14

将第一个EditText的值放入第二个EditText的值中时,您可以对Strings使用Java的替换()函数。例如,以下代码打印“$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);
和丰羽
2023-03-14

将Textwatcher分配给您的editText,然后执行以下操作

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

这是应用了代码的text Watcher

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

错误可能在于您再次获取旧字符串并应用文本。更换。而不是修改后的字符串。我本以为它会得到新字符串,但setText似乎没有立即启动。

因此,如果将字符串转换为变量,则应用所有更改。然后把它放回你的文本框,它会正常工作。(我已在此处测试和工作代码粘贴在上面)

 类似资料:
  • 我使用TextWatcher来更改按下的键值。我的目标是在打字时替换一些字符。例如当我键入键时,如果到达“S”字符,就用“a”字符替换它。我的问题是:我应该在beforeTextChanged中做吗??怎么会?有人能给我举个例子吗?

  • 我正在使用一个具有许多不同库依赖关系的gradle项目,并使用新的清单合并。在我的

  • 有没有人知道如何使用JSoup替换元素。我试图用按钮替换表格元素及其内容,但没有成功。代码尝试如下。这是一个Android项目

  • 每当“q”被写为我附加TextWatcher的edittext的最后一个字符时,这个“q”就会被替换为“a”。我使用: 但是,当我测试代码时,当我输入“q”时,什么也没发生。一些帮助?非常感谢。

  • 我已经知道有很多问题都在问同样的问题,但它们对我不起作用- 它给出堆栈溢出错误。 有用但不合适的答案- 1)添加前的代码和添加后的代码-这一个作品,但不正常,应用程序变得非常慢。 还有其他方法可以帮我吗?谢谢。

  • 在Android应用程序中,我有一个< code>EditText,它应该用应用程序数据中的值替换某些字符串。 例如,如果用户键入则应将其替换为当前登录的用户的名称。 在 的 方法中的可编辑参数将替换为正确的值,但问题是,如果我按下任何字符,被替换为实际用户名之后,它会附加后跟按下的字符。 e. g. 假设当前登录的用户名是 a、 如果输入 b.< code>afterTextChanged()将