I have a functioning button with increment and decrement functioning correctly. but I have the need that I can also enter the value manually with an EdidText.
My code is the following:
Activity.tk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal var minteger = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val plus = findViewById<Button>(R.id.increase)
val minus = findViewById<Button>(R.id.decrease)
plus.setOnClickListener {
increaseInteger(plus)
}
minus.setOnClickListener {
decreaseInteger(minus)
}
}
fun increaseInteger(view: View) {
minteger += 1
display(minteger)
}
fun decreaseInteger(view: View) {
minteger -= 1
display(minteger)
}
private fun display(number: Int) {
val displayInteger = findViewById<View>(
R.id.integer_number) as TextView
displayInteger.text = "" + number
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:orientation="horizontal">
<Button
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="16dp"
android:text="0"
android:textStyle="bold"
android:textSize="70sp" />
<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
I have changed the TextView with an EditText (@+id/integer_number)
But when I enter a value that is not saved, I can not find a way to make the value entered in the EdidText saved so that I can increase or decrease it.
I will appreciate any suggestion to be able to operate my button.
