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

只愿长相守 提交于 2019-12-11 19:06:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!