Home > Software design >  How to save number from EditView in another integer in kotlin
How to save number from EditView in another integer in kotlin

Time:03-23

I'm pretty new to android so I will try to explain this the best way I can,

so I have a EditView in an activity I created like this:

<EditText
        android:id="@ id/num"
        android:layout_width="230dp"
        android:layout_height="61dp"
        android:inputType="number"
        android:background="@drawable/border"
        android:layout_marginTop="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView"
        app:layout_constraintVertical_bias="0.129" />

and in my MainActivity.kt I save the number user input's like this:

val number = findViewById<EditText>(R.id.num)

but when I try to sum the number user input's with a number I declared in my code like this:

var summ = 1

summ  = number

When I launch program I says this:

None of the following functions can be called with the arguments supplied: public final operator fun plus(other: Byte): Int defined in kotlin.Int public final operator fun plus(other: Double): Double defined in kotlin.Int public final operator fun plus(other: Float): Float defined in kotlin.Int public final operator fun plus(other: Int): Int defined in kotlin.Ints public final operator fun plus(other: Long): Long defined in kotlin.Int public final operator fun plus(other: Short): Int defined in kotlin.Int

If anyone knows how I could save the number user input's with a number I save in my MainActivity.kt please let me know!

CodePudding user response:

Using this should work:

val number = findViewById<EditText>(R.id.num).text.toString().toInt()

Since findViewById<EditText>(R.id.num) returns a View not an Int.

CodePudding user response:

Try this:

val number = findViewById<EditText>(R.id.num).text.toString().toInt()
  • Related