Setting text in EditText Kotlin

2019-02-07 15:44发布

I am trying to set text in a edittext but it says Type mismatch Required Editable found string my code is as follow

String name = "Paramjeet"
val nametxt = findViewById (R.id.nametxt) as EditText
nametxt.text = name

Dont say to use SetText because i am using kotlin not java

9条回答
Ridiculous、
2楼-- · 2019-02-07 16:32

Look at the API of EditText:

void setText (CharSequence text, TextView.BufferType type)

Editable getText ()

Link: https://developer.android.com/reference/android/widget/EditText.html

查看更多
叼着烟拽天下
3楼-- · 2019-02-07 16:34
editText.setText("Your text here")
查看更多
看我几分像从前
4楼-- · 2019-02-07 16:38

Use setText(String) as EditText.text requires an editable at firstplace not String

WHY ?

Nice explanation by Michael given under this link. Do visit this link for more detail

When generating a synthetic property for a Java getter/setter pair Kotlin first looks for a getter. The getter is enough to create a synthetic property with a type of the getter. On the other hand the property will not be created if only a setter presents.

When a setter comes into play property creation becomes more difficult. The reason is that the getter and the setter may have different type. Moreover, the getter and/or the setter may be overridden in a subclass.

查看更多
登录 后发表回答