如何在用户选中时指定整数值复选框

时间:2014-09-30 08:05:28

标签: java android eclipse checkbox addition

我现在正面临这个问题。我想生成一份食物收据及其价格和总价(所有选中复选框的总和 - 所选菜单)

这是复选框页面的代码,用户需要检查他们想要购买的商品。

                    <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" />

这是流程的代码

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

这是显示收据的过程

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

**我希望两个选中项目的总价格在收据中显示为总计

这是显示收据的页面

   <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" />

帮帮我PLZ ....

2 个答案:

答案 0 :(得分:0)

你不清楚这些价值从何而来。

但你可以做的是:

//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)

希望这有帮助。

答案 1 :(得分:0)

您还可以扩展CheckBox并添加price float属性并在构造函数中设置其值,然后创建一个返回0或this.price的新方法getPrice,具体取决于是否检查。

伪码

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.

然后简单地使用PriceCheckBox而不是CheckBox并调用getPrice()而不是isChecked()。