Ad

Plus And Minus Button With EditText

- 1 answer

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.

buton

Ad

Answer

In your xml change to the EditText:

<EditText
    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:inputType="number"
    android:textStyle="bold"
    android:textSize="70sp" />

I added the attribute:

android:inputType="number"

so only digits are allowed.
In your MainActivity class add this import:

import kotlinx.android.synthetic.main.activity_main.*

so you can access all the items of the xml without the need of findViewById().
Now you don't need to save the value of the EditText anywhere because you can get it anytime by:

integer_number.text.toString().toInt()

so the variable minteger is no longer needed.
Change the activity's code to this:

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        increase.setOnClickListener { increaseInteger() }
        decrease.setOnClickListener { decreaseInteger() }
    }

    fun increaseInteger() {
        display(integer_number.text.toString().toInt() + 1)
    }

    fun decreaseInteger() {
        display(integer_number.text.toString().toInt() - 1)
    }

    private fun display(number: Int) {
        integer_number.setText("$number")
    }
}

I made other simplifications too.
You don't need any arguments for the functions increaseInteger() and decreaseInteger().
There is a limitation on the maximum and minimum integer values.
So if you want to be on the safe side you must also use a try/catch block inside increaseInteger() and decreaseInteger() to avoid exceptions.

Ad
source: stackoverflow.com
Ad