问题
I have got this error on Android Studio IDE. It says
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})", "$1-$2")
}
}
回答1:
editabletext is an EditText and replaceAll returns a String. What the compiler says is that you can't assign a String to an EditText. You could change your code this way:
if(editabletext.length() == 8){
String tmp = editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "$1-$2");
editabletext.setText(tmp);
}
回答2:
Replace
editabletext= editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "$1-$2")
with
editabletext.setText(editabletext.getText().toString().replaceAll("([0-9]{4})([0-9]{4})", "$1-$2"))
as editabletext is of type Editable and replaceAll returns a String
回答3:
find the definition of "editabletext" and change the type from EditText to String
来源:https://stackoverflow.com/questions/29052935/incompatible-types-android-widget-edit-text-and-java-lang-string