如何在Android中使用EditText用户输入,递增和递减按钮值

时间:2018-07-12 10:55:06

标签: java android android-edittext increment decrement

我是android的新手。因此,我尝试使用EditText而不是ListView计算某物的价格,并且包含增加和减少按钮以增加EditText组件上的值。

因此,递增和递减按钮可以正常工作。他们在递增和递减EditText中的值,并计算价格就好了,但是当我键入EditText而不是使用按钮时,输入的值不会被使用。

这是我的代码。

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);
    }
}

请问有人可以帮助我吗?

7 个答案:

答案 0 :(得分:0)

使用quantityText.getText()函数获取该值并将其存储在变量中,然后对该值进行递增和递减运算。

类似这样的东西:

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

,然后将此value传递给您的递增和递减函数。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用case "1"

答案 3 :(得分:0)

尝试:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getSubtype() 

答案 4 :(得分:0)

它很简单,用于创建onclick事件:

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());
         }
     });

答案 5 :(得分:0)

之所以不使用EditText的原因是因为您没有从EditText获取值。因此获得这样的值;

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

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

现在使用该整数值执行所需的任何操作

祝你好运:)

答案 6 :(得分:0)

    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