Incompatible types: android.widget.Edit Text and j

2019-08-26 08: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")
    }
}

3条回答
干净又极端
2楼-- · 2019-08-26 08:31

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);
  }
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-26 08:34

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-26 08:40

find the definition of "editabletext" and change the type from EditText to String

查看更多
登录 后发表回答