简体   繁体   中英

Setting and reading an EditText in Kotlin

I have a simple issue (I hope).

I have two preset values (IP and port) that I need the app user to be able to change. I have two values in a class.

public class SoftOptions {
    var RemoteHost: String = "192.168.43.237"
    var RemotePort: Int = 1234

}

And then of course val mySettings = SoftOptions().

I then show them in my MainActivity as text fields. Then, upon pushing the Settings button, I go to an activity called Settings. In it, I place the values to the edit boxes so as not to have to change the entire IP address if only the last digit changes. I have managed this too.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    var editEnterip =  mySettings.RemoteHost
    var editEnterport= mySettings.RemotePort
    setContentView(R.layout.activity_settings)
    this.editEnterip.setText(mySettings.RemoteHost.toString())
    this.editEnterport.setText(mySettings.RemotePort.toString())

But then, in the button listener, I just need to read the new value and assign that to the mySettings.RemoteHost and mySettings.RemotePort variables.

   btnSetip.setOnClickListener {
        var finalIP  =findViewById<EditText>(R.id.editEnterip)
        Toast.makeText(this, finalIP.toString(), Toast.LENGTH_SHORT).show()
        myTargetIP = finalIP.toString()
        mySettings.RemoteHost = finalIP.toString()

        editEnterport =  editEnterport .  toString () .toInt()
        myTargetPort = "$editEnterport"
        mySettings.RemotePort = myTargetPort.toString().toInt()
        val intent = Intent(this, MainActivity::class.java)
        intent.putExtra("myTargetIP", myTargetPort)
        intent.putExtra("myTargetPort", myTargetPort)
        startActivity(intent)
    }

But this, while not crashing the app, puts a godawful error message as the value of the remote host, even if the edited IP value is fine.

So, my question is merely, what am I doing wrong to not get a clean text value out of the edit box?

This is the message in my user interface. And many thanks in advance, this forum is the most powerful of any I use.

My user interface

In order to get text from edittext you have to use getText() method of edittext class you can refer this for more details https://developer.android.com/reference/android/widget/EditText#getText()

getText() will than return you Editable on which you have to apply toString() method that will give you editext text in string.

Considering your code in question you can get it in following way

var finalIP =findViewById<EditText>(R.id.editEnterip)
var textFromEditText = finalIP.text.toString() // access text this way
println(textFromEditText)

As you are using kotlin so there is no need to write full getText() thanks to it's property access syntax, you can refer here for more details https://kotlinlang.org/docs/reference/properties.html

Change:

   myTargetPort.toString().toInt()

to:

   myTargetPort.text.toString().toInt()

Do the same for all EditText fields you have.

edittext.toString() returns the default Object#toString() implementation that prints its address as result, while .text.toString() gets the text within the edittext as you want to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM