简体   繁体   中英

how to assign an integer value when user checked check box

i am facing this problem right now. i want to generate a receipt of food along with its price and total price (summation of all checked check box-the selected menu)

this is the code for the check box page where user need to checked on items that they would like to buy.

                    <RelativeLayout >

                    <ScrollView 
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical" >

                           <CheckBox
                            android:id="@+id/pakejC1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Beg" />

                            <CheckBox
                            android:id="@+id/pakejC2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Shoes" />




                        <ImageButton
                                android:id="@+id/gobutton"
                                android:layout_width="50dp"
                                android:layout_height="50dp"
                                android:layout_alignTop="@+id/homebtn"
                                android:layout_toRightOf="@+id/homebtn"
                                android:background="@drawable/gobutton"
                                android:onClick="goReceiptC" />

this is the code for the process

   public class item extends Activity
        {

    CheckBox beg1, shoes1;


    public void onCreate(Bundle savedInstanceState) 
    {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.item);

  beg1       = (CheckBox) findViewById(R.id.pakejC1);
  shoes1     = (CheckBox) findViewById(R.id.pakejC2);
  }



public void goReceiptC(View v) 
        {
            Intent intent = new Intent(v.getContext(), doReceiptC.class);

            intent.putExtra("beg1", beg1.isChecked());
            intent.putExtra("shoes1", shoes1.isChecked());

         startActivityForResult(intent,0);
        }
  }

this is the process for the display receipt

  public class doReceipt extends Activity

{
  boolean beg1, shoes1;

   TextView tvOutput1,tvOutput2;

    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.receipt);



      tvOutput1 = (TextView) findViewById(R.id.textView1);
      tvOutput2 = (TextView) findViewById(R.id.textView2);

      Bundle data = this.getIntent().getExtras();

      beg1=data.getBoolean("beg1");
      shoes1=data.getBoolean("shoes1");

      if(beg2==true)
      {

          tvOutput1.setText("Nasi Putih RM 1.00");
           tvOutput1.setVisibility(View.VISIBLE);

  // this is where i would like to display the price
   //eg rm 1.00


      }
      else
      {
          tvOutput1.setVisibility(View.GONE);
      }

      if (shoes1==true)
      {
          tvOutput2.setText("shoes RM 1.50");
          tvOutput2.setVisibility(View.VISIBLE);

        // this is where i would like to display the price
   //eg rm 1.50


      }
      else
      {
          tvOutput2.setVisibility(View.GONE);
      }

** i want the total price for both checked item to be display in the receipt as total

this is the page for display the receipt

   <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textA"
    android:layout_below="@+id/textA"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
   android:text="total price"
    android:textAppearance="?android:attr/textAppearanceMedium" />

help me plz....

You are not very clear about from where are these values coming.

But what you can do is:

//global variables
float bagPrice = 1.0f, shoesPrice = 1.5f, total = 0f;

//your logic of getting values
if(beg2==true)
{
    //do what you want in textviews
    total += bagPrice;
}
else
{
    //your code
}

if (shoes1==true)
{
    //do what you want in textviews
    total += shoesPrice;
}
else
{
    //your code
}

Toast.makeText(getApplicationContext(), String.valueOf(total), Toast.LENGTH_SHORT).show(); //show total in TextView using String.valueOf(total)

Hope this helps.

You could also extend CheckBox and add a price float attribute and set its value in the constructor, then create a new method getPrice that return 0 or this.price depending of whether its checked or not.

Pseudo code

public class PriceCheckBox() extends CheckBox {

private float price;

public PriceCheckBox(float price) {
    super():
    this.price = price;
}

public float getPrice() {
    if(isChecked()){
        return this.price;
    } else {
        return 0;
    }
}

//You could also add a setter here if you wish to change price later.

Then simply use PriceCheckBox instead of CheckBox and call getPrice() instead isChecked().

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