简体   繁体   中英

How to use EditText user input, increment and decrement buttons values in android

I'm kinda new to android. So I'm trying calculate the price of something using EditText instead of ListView, and I have increment and decrement buttons included to increase the value on the EditText component.

So the increment and decrement buttons are working okay. They are decrementing and incrementing the value in EditText, and are calculating the price just fine, but when I type in the EditText instead of using the buttons the value I typed doesn't get used.

Here's my Code.

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/latte" />

    <TextView
        android:id="@+id/quantitytxt_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Quantity ( Cup(s) )"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/decrementButton"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:onClick="decrement"
        android:text="-"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/quantity_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:layout_toRightOf="@id/decrementButton"
        android:padding="10dp"
        android:inputType="text"
        android:text="0"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/incrementButton"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:layout_toRightOf="@id/quantity_textview"
        android:onClick="increment"
        android:text="+"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/pricetxt_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantity_textview"
        android:padding="10dp"
        android:text="Price (1 Cup = KES 5)"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/price_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pricetxt_textview"
        android:padding="10dp"
        android:text="KES 0"
        android:textColor="@android:color/white"
        android:textSize="16sp" />


    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/price_textview"
        android:onClick="submitOrder"
        android:text="Order"
        android:textSize="15sp" />

</RelativeLayout>

Java

package com.teqqli.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;


/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    int quantity = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int price = quantity*5;
        displayPrice(price);
    }

    public void increment (View view) {
        quantity = quantity + 1;
        display(quantity);
    }

    public void decrement (View view) {
        if (quantity>0){
            quantity = quantity - 1;
            display(quantity);
        }
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        EditText quantityText = (EditText) findViewById(R.id.quantity_textview);
        quantityText.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_textview);
//        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
        priceTextView.setText("KES " + number);
    }
}

Kindly can anyone assist me?

Use quantityText.getText() function to get the value and store it in a variable and then do the increment and decrement operation on this value.

Something like this:

int value = Integer.parseInt(quantityText.getText().toString().trim());

and then pass this value to your increment and decrement functions.

String yourValue = quantityText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(yourValue); //displays it in a textview..

使用quantity = Integer.parseInt(quantityText.getText().toString());

Try:

public class MainActivity extends AppCompatActivity {

    EditText quantityText;
    int quantity = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        quantityText= (EditText) findViewById(R.id.quantity_textview);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        quantity = Integer.parseInt(quantityText.getText().toString());
        int price = quantity*5;
        displayPrice(price);
    }

    public void increment (View view) {
        quantity = quantity + 1;
        display(quantity);
    }

    public void decrement (View view) {
        if (quantity>0){
            quantity = quantity - 1;
            display(quantity);
        }
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        quantityText.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_textview);
        priceTextView.setText("KES " + number);
    }
}

its simple, for create onclick event:

final EditText input = (EditText) findViewById(R.id.textview_id);
final TextView view = (TextView) findViewById(R.id.textview_id);
final Button button = findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             view.setText(input.getText().toString());
         }
     });

The reason your EditText doesn't get used is because you are not getting the value from EditText. So get that value like;

String valueFromEditText=quantityText.getText().toString(); // Getting string value from EditText

int valueFromEditTextInIntegerFormat=Integer.parseInt(valueFromEditText) // Converting string value received into integer format

Now use this integer value for performing whatever operations you want

Good Luck :)

    You need to create this method: 
       private void display()
        {
            EditText quantityText = (EditText) findViewById(R.id.quantity_textview);



                int  value = Integer.parseInt(quantityText.getText().toString());
                quantity=value;
                Log.i("vl", "display: "+value);

        }
    //replace your method with this 
        public void submitOrder(View view) {
            if(quantity==0)
            {
                display();
            }
            int price = quantity*5;
            quantity=0;
            displayPrice(price);
        }
//if you get any problem ask me in a comment

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