Setting text in EditText Kotlin

℡╲_俬逩灬. 提交于 2019-11-30 00:12:21

Use setText(String), since editText.text expects an Editable, not a String.

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.

If you want to use getter .text from principle, use:

nametxt.text = Editable.Factory.getInstance().newEditable(name)

There are several working answers here, but if you still want to use the property format and have your code look clean, you could write an extension:

fun String.toEditable(): Editable =  Editable.Factory.getInstance().newEditable(this)

You can then use it as such:

mEditText.text = myString.toEditable()
letroll

Or you can use an extension property:

var EditText.value
    get() = this.text.toString()
    set(value) {
            this.setText(value)
    }

and use .value= instead of .text=

Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin. But, While generating a property for a Java getter/setter pair Kotlin at first looks for a getter. The getter is enough to infer the type of property from the type of the getter. On the other hand, the property will not be created if only a setter is present( because Kotlin does not support set-only properties at this time ) .

When a setter comes into play, property generation process becomes a bit ambiguous. 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 , which exactly is the case of EditText in android.

In above case the Android TextView class contains a getter

CharSequence getText() 

and a setter void

setText(CharSequence)

If I had a variable of type TextView my code would have worked fine. But I used EditText class which contains an overridden getter

Editable getText()

which means that you can get an Editable for an EditText and set an Editable to an EditText. Therefore, Kotlin reasonably creates a synthetic property text of type Editable. As String class is not Editable, that’s why I cannot assign a String instance to the text property of the EditText class.

It Seems like JetBrains forgot to specify the dominant role of getter methods while generating kotlin properties for Java getter and setter methods. Anyways, I have submitted pull request to Jet brains kotlin website through github.

I have detailed above problem in this medium post too How Does Kotlin Generate Property from Java Getters and Setters (Undocumented by Jetbrains)

editText.setText("Your text here")

setText(String), so you must setText your string to editText, so in your case is : nametxt.setText(name)

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

Try using nametxt.post: nametxt.post({nametxt.setText("your text")})

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