简体   繁体   中英

Menu actions and sums in Android app

So I have been trying to develop an app that adds a select quantity together in a menu, all while in Android studio/eclipse. I am listing off drinks and to the right of each drink is a "-" (the minus) and "+" (the plus) signs, which are buttons respectively, with a "0" in the middle. At the bottom of the screen, I have a "Total" section.

I have a screen shot of what the menu appears as thus far:

drinkmenu

I am looking to have the + and - buttons affect the number that lies in the middle of them. So when the "+" is pressed it adds 1 and if pressed again, it adds 1 and so on, the "-" button will take 1 away from the number back to zero if need be.

It should be able to tally the total price of each drink all together and output the sum to the "$ 0.00" near the total. The "Order" button just takes me to another screen, which will be tackled at a later date.

I have tried searching for a good many hours and there are so many links I am drowning that I can not find any help that sticks. I know that the Java code should use the ID of the EditText's but I am confused as to how.

I will post what code I have for the xml and java code. If there is also a manifest I have to add, I can not find it.

drinkmenu.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/mainmenulayout"
android:background="#34D6D9">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Drinks:"
    android:id="@+id/textView"
    android:background="#FFFFFF"
    android:textStyle="bold"
    android:textSize="24dp"
    android:layout_toStartOf="@+id/textView2"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_marginRight="75dp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Quantity:"
    android:id="@+id/textView2"
    android:textSize="24dp"
    android:textStyle="bold"
    android:background="#FFFFFF"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/orderbtn"
    android:layout_alignEnd="@+id/orderbtn" />

<Button
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:text="Espresso"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"

    android:layout_marginTop="30dp"
    android:background="#FFFFFF"
    android:textSize="24dp" />

<Button
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:text="Macciato"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:textSize="24dp"
    android:background="#FFFFFF" />

<Button
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:text="Con Panna"
    android:id="@+id/button3"
    android:layout_below="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:background="#FFFFFF"
    android:textSize="24dp" />

<Button
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:text="Americano"
    android:id="@+id/button4"
    android:layout_below="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:textSize="24dp"
    android:background="#FFFFFF" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:text="Latte"
    android:id="@+id/button5"
    android:layout_below="@+id/button4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:background="#FFFFFF"
    android:textSize="24dp" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#ffff45df"
    android:textSize="24dp" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_alignTop="@+id/button"
    android:layout_toLeftOf="@+id/button6"
    android:layout_toStartOf="@+id/button6"
    android:text=" 0"
    android:textSize="24dp"
    android:background="#FFFFFF"
    android:layout_above="@+id/button2" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button7"
    android:layout_alignTop="@+id/button"
    android:layout_toLeftOf="@+id/editText2"
    android:layout_toStartOf="@+id/editText2"
    android:background="#ffff45df"
    android:textSize="24dp" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button8"
    android:background="#ffff45df"
    android:textSize="24dp"
    android:layout_alignTop="@+id/button2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_toStartOf="@+id/button8"
    android:text=" 0"
    android:textSize="24dp"
    android:background="#FFFFFF"
    android:layout_alignTop="@+id/button2"
    android:layout_toLeftOf="@+id/button8" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText4"
    android:layout_toStartOf="@+id/button8"
    android:text=" 0"
    android:textSize="24sp"
    android:background="#FFFFFF"
    android:layout_alignTop="@+id/button2"
    android:layout_toLeftOf="@+id/button8"
    android:layout_alignBottom="@+id/button8" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button9"
    android:layout_toStartOf="@+id/editText3"
    android:background="#ffff45df"
    android:textSize="24dp"
    android:layout_alignTop="@+id/button2"
    android:layout_toLeftOf="@+id/editText3" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button10"
    android:background="#ffff45df"
    android:textSize="24dp"
    android:layout_alignTop="@+id/button3"
    android:layout_alignRight="@+id/button8"
    android:layout_alignEnd="@+id/button8" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button11"
    android:background="#ffff45df"
    android:textSize="24dp"
    android:layout_alignTop="@+id/button4"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button12"
    android:background="#ffff45df"
    android:textSize="24dp"
    android:layout_alignTop="@+id/button5"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText5"
    android:layout_toStartOf="@+id/button10"
    android:text=" 0"
    android:textSize="24dp"
    android:background="#FFFFFF"
    android:layout_alignTop="@+id/button3"
    android:layout_toLeftOf="@+id/button10"
    android:layout_above="@+id/button4" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText6"
    android:layout_toStartOf="@+id/button11"
    android:text=" 0"
    android:textSize="24dp"
    android:background="#FFFFFF"
    android:layout_alignTop="@+id/button4"
    android:layout_toLeftOf="@+id/button11"
    android:layout_alignBottom="@+id/button11" />

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText7"
    android:layout_toStartOf="@+id/button12"
    android:text=" 0"
    android:textSize="24dp"
    android:background="#FFFFFF"
    android:layout_alignTop="@+id/button5"
    android:layout_toLeftOf="@+id/button12"
    android:layout_alignBottom="@+id/button12" />



<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button14"
    android:layout_toStartOf="@+id/editText6"
    android:background="#ffff45df"
    android:textSize="24sp"
    android:layout_alignTop="@+id/button4"
    android:layout_toLeftOf="@+id/editText6" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button15"
    android:layout_toStartOf="@+id/editText7"
    android:background="#ffff45df"
    android:textSize="24sp"
    android:layout_alignTop="@+id/button5"
    android:layout_toLeftOf="@+id/editText7" />

<Button
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button16"
    android:layout_toStartOf="@+id/editText5"
    android:background="#ffff45df"
    android:textSize="24sp"
    android:layout_alignTop="@+id/button3"
    android:layout_toLeftOf="@+id/editText5" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText8"
    android:text="Total:"
    android:background="#FFFFFF"
    android:textSize="24sp"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/textView"
    android:layout_toStartOf="@+id/textView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText9"
    android:layout_alignBottom="@+id/editText8"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView"
    android:text="      $ 0.00"
    android:background="#FFFFFF"
    android:textSize="24sp"
    android:layout_alignTop="@+id/editText8" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Order"
    android:id="@+id/orderbtn"
    android:textSize="24sp"
    android:background="@drawable/buttonround"
    android:layout_alignBottom="@+id/editText9"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="$3.00 per drink" 
    android:textColor="#000000"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button9"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="$3.00 per drink"
    android:textColor="#000000"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button16"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="$3.00 per drink" 
    android:textColor="#000000"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button14"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="$3.00 per drink" 
    android:textColor="#000000"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button15"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="$3.00 per drink" 
    android:textColor="#000000"
    android:textSize="20sp"/>

</RelativeLayout>

java code: package com.example.cofeeshop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class DrinkMenu extends Activity {

Button button;
Button buttton6;  Button buttton7;  Button buttton8;  Button buttton9;
Button buttton10; Button buttton11; Button buttton16; Button buttton14;
Button buttton12; Button buttton15;

// EditText editText2; EditText editText4; EditText editText5;
// EditText editText6; EditText editText7; EditText editText9;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drinkmenu);
    addListenerOnButton();
}

private void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.orderbtn);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, ThirdPartyPaymentMethod.class);
            startActivity(intent);

            }

        });
    }
}

I already have my manifest to allow button actions.

I know there are methods out there, but my searches have ended in my frustration. Any help would be appreciated. I am new to this, I enjoy the front end, but the back end is new.

I'll do a part and for remaining you have to do.

Consider,

- is a button with id 'button1' and '+' has button2
Quantity is a EditText which has id qty.

Right, coming to the code :

public class MainActivity extends Activity {

EditText quantity;
Button plus,minus;

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

    plus = (Button) findViewById(R.id.button1);
    minus = (Button) findViewById(R.id.button2);

    quantity = (EditText) findViewById(R.id.qty);

    plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String numb = quantity.getText().toString();
            int num1 = Integer.parseInt(numb);
            int inum = num1+1;
            quantity.setText(Integer.toString(inum));
        }
    });

 minus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String numb = quantity.getText().toString();
            int num1 = Integer.parseInt(numb);
            int inum = num1-1;
            quantity.setText(Integer.toString(inum));
        }
    });
  } 
}

For sub total, you just need to multiply quantity and the item price . For the Total Price , you just need to add all the subtotals. Hope it helps!

UPDATE about Catching Zero :

You are getting the quantity here.

 String numb = quantity.getText().toString();
 int num1 = Integer.parseInt(numb);

So, if the number is 0, perform if else

if (num1==0)
 {
    //Create a alert showing your quantity is zero.
    //here i use toast to display

   Toast.makeText(this,"Your quantity is Zero. You can't reduce the quantity", Toast.LENGTH_LONG).show();

  }

 else

 {

    // do the stuffs here

  }

what you want to do is something like this:

Button button = (Button) findViewById(R.id.my_button);
final TextView textView = (TextView) findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener() {
  @Overrride
  public void onClick(View aView) {
      textView.setText("Hi");
  }
});

You can write this in the onCreate() method of the Activity. Not tested yet, but this will change the text of the TextView to the desired value. Let me know if I have understood your problem correctly.

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