Home > Software design >  Kotlin / Android Studio - Beginner
Kotlin / Android Studio - Beginner

Time:08-20

I had hard case to understand how it works, basically i cannot write code which text from form will be save in variable and after it printed... I saw video on youtube, tons of faq blogs but they are using fully java and code in wrong or old formulas....

I have:

activityPlay.xml

<EditText
    android:id="@ id/imie"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    tools:layout_editor_absoluteX="182dp"
    tools:layout_editor_absoluteY="281dp" />



<Button
    android:id="@ id/submit"
    android:layout_width="265dp"
    android:layout_height="115dp"
    android:text="Button"
    tools:layout_editor_absoluteX="216dp" />

and

MainActivity.kt

val submit = findViewById<Button>(R.id.submit)

val imie = findViewById<EditText>(R.id.imie)
submit.setOnClickListener {
        val userText = imie.text.toString()

    }

May you help me and clearly fix the code for save text to value... ?

CodePudding user response:

try this:

val imie = findViewById(R.id.imie) as EditText
submit.setOnClickListener {
    val userText = imie.text

}

CodePudding user response:

It looks like you managed to save the data into a variable. But you don't use it afterward.

Change

submit.setOnClickListener {
        val userText = imie.text.toString()

    }

to

submit.setOnClickListener {
        val userText = imie.text.toString()
        submit.text = userText
    }

This will change the text on the btn

  • Related