不兼容的类型:android.widget.Edit 文本和 java.lang.String

Incompatible types: android.widget.Edit Text and java.lang.String

我在 Android Studio IDE 上遇到了这个错误。它说

Incompatible types; Required android.widget.EditText and Found java.lang.String

@Override
public void afterTextChanged(Editable s) {

    if (editabletext.length() == 8) {
        editabletext = editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "-")
    }
}

editabletext 是一个 EditText,replaceAll returns 是一个字符串。编译器说的是您不能将 String 分配给 EditText。您可以这样更改代码:

if(editabletext.length() == 8){
    String tmp = editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "-");
    editabletext.setText(tmp);
  }

替换

editabletext= editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "-")

editabletext.setText(editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "-"))

as editabletext 是类型 Editable and replaceAll returns a String

找到"editabletext"的定义,将类型从EditText改为String