Convert String obtained from edittext to Integer in Kotlin language

Viewed 22881

I am trying to make a simple Android application using Kotlin language. I have one EditText, I am getting its value in String but I want to convert that value into an integer. How can I convert this string to integer in Kotlin language?.

3 Answers

The above is the general idea but here is a syntax straight out of Android Studio, from a different tutorial I'm doing.

Note that the compiler was perfectly happy to do a cast of a cast.

var myNewInt: Int = myEditTextView.text.toString().toInt()

the returned value of (edittext.text) in kotlin is a editable? value and at the first you must convert that to string

edittext.text.toString().toInt()

Related