Android Studio:替换edittext文本

时间:2018-11-05 04:46:38

标签: android-studio android-edittext

在我的Android Studio项目中,我设计了一个editText并单击按钮。 当用户在editText中输入一些文本并单击按钮editText时,其他字符将被替换。

  EditText  small=(EditText)findViewById(R.id.editText);
                String str = small.getText().toString();
               str=str.replace("nys","La");
            str=str.replace("iy","Lai");
            str=str.replace("Ny","Li");
            str=str.replace("Nyh","Lo");
            str=str.replace("yh","Low");
            str=str.replace("Y}","L");
            str=str.replace("yP","LA");
            str=str.replace("y","Lu");
//Likewise has lot of vice versa replaces
 EditText capital=(EditText)findViewById(R.id.editText);
                capital.setText(str);

这是正确的吗?

我可以将此替换函数放置到另一个类中,然后在这里调用..吗?我该怎么办?

1 个答案:

答案 0 :(得分:0)

是的,您可以执行此操作,在您的一个类中创建一个静态函数并在当前类中调用。

定义常用功能

public static String replaceCharacters(String inputString) {
    inputString=inputString.replaceAll("nys","La");
    inputString=inputString.replaceAll("iy","Lai");
    inputString=inputString.replaceAll("Ny","Li");
    inputString=inputString.replaceAll("Nyh","Lo");
    inputString=inputString.replaceAll("yh","Low");
    inputString=inputString.replaceAll("Y}","L");
    inputString=inputString.replaceAll("yP","LA");
    inputString=inputString.replaceAll("y","Lu");
    return inputString;
} 

从您的活动班级打来电话

EditText small=(EditText)findViewById(R.id.editText);
EditText capital=(EditText)findViewById(R.id.editText);
capital.setText(""+YourClassName.replaceCharacters(small.getText().toString()));