选中复选框到另一个活动的值(名称,价格)

时间:2019-06-02 00:23:21

标签: android android-studio android-intent checkbox

使用Java Andriod工作室 在屏幕上显示相应的食物。 例如,蔬菜屏幕将显示羽衣甘蓝,洋葱,... 使用复选框显示食物。 然后选定的复选框食品项目和价格将显示在结帐屏幕上(另一项活动)

(将一个活动中选中的复选框的值(总价,名称)发送到另一个活动)

下面是我现在拥有的

public class Vegetables extends FoodTypesMenu  {

    CheckBox cb1, cb2, cb3, cb4;

    String name = "";
    String vegprice = "";

    double vegPrice = 0;

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



    }

    public void onCheckboxClicked(View view) {
        //Is the button now checked?
        boolean checked = ((CheckBox) view).isChecked();

        Button btnNext = (Button) findViewById(R.id.bn1);

        switch (view.getId()) {
            case R.id.onion_cb:
                if(checked){
                   name="Onion";
                   vegPrice += 5;

                }
                break;
            case R.id.carrot_cb:
                if(checked){
                  name="Carrot";

                }

                break;
            case R.id.spinach_cb:
                if(checked){
                    name="Spinach";


                }
                break;
            case R.id.garlic_cb:
                if(checked){
                    name="Garlic";


                }
                break;

        }

    }

    public void tocheckout(View v) {

        Intent intent = new Intent(this, CheckOut.class);
        intent.putExtra("Name", name);
        startActivity(intent);
        startActivity(intent);

    }

    }
```

```
public class CheckOut extends FoodTypesMenu {



    String name = "";
    String price = "";


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



        String name = getIntent().getStringExtra("Name");

        }

        public void OnClickButton (View view){
            Intent intent2 = new Intent(this, Payment.class);
            this.startActivity(intent2);
    }




    }



The string name is not displayed and also don't know how to display total price of checked boxes on another activity

[enter image description here][2]


  [1]: https://i.stack.imgur.com/Mag7P.png
  [2]: https://i.stack.imgur.com/1kw6X.png

1 个答案:

答案 0 :(得分:0)

尝试一下

 // modify your tocheckout() method
  public void tocheckout(String name,double vegPrice) {
    Intent intent = new Intent(this, CheckOut.class);
    intent.putExtra("name",name);
    intent.putExtra("vegPrice",vegPrice);
    startActivity(intent);

}
// call the above function from your onclick of checkbox
  switch (view.getId()) {
        case R.id.onion_cb:
            if(checked){
               name="Onion";
               vegPrice += 5;
               tocheckout(name,vegPrice);
            }
            break;
}


// and final get name and price in your CheckOut activity like this
  String name=getIntent().getStringExtra("name");
  double price=getIntent().getDoubleExtra("vegPrice",-1); //-1 is default val