GeoCap
GeoCap

Reputation: 515

Convert EditText (type phone) to String in Kotlin

I have two EditText input fields (inputType="phone") one for IP address and one for port number. I want to get the two values in a string form.

val ip : String = findViewById<EditText>(R.id.ip).toString()
val port: String = findViewById<EditText>(R.id.port).toString()
println("IP AND PORT: $ip : $port")

The ouput is something like: androidx.appcompat.widget.AppCompatEditText...

Upvotes: 3

Views: 1868

Answers (1)

iknow
iknow

Reputation: 9892

You are converting EditText to String. It is complex object not just a visible text. To get text from EditText You have to get text field from TextView. Like this:

val ip : String = findViewById<EditText>(R.id.ip).text.toString()
val port: String = findViewById<EditText>(R.id.port).text.toString()

textView.text is not a String but Editable, that's why You have to add .toString()

Upvotes: 5

Related Questions