选中单选按钮或复选框时,将计算价格

时间:2018-11-09 12:37:17

标签: android-studio checkbox radio-button

我正在创建一个允许用户自己选择的应用。

我可以知道如何进行计算吗?单击单选按钮或复选框时,似乎不包含价格。请帮我。是逻辑上错误还是语法错误?

radioGroup1=(RadioGroup)findViewById(R.id.radioGroup_1);

radioGroup1.setOnCheckedChangeListener(
    new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            String tempSize="Choose your size";
            double SPrice=0;
            switch (radioGroup1.getCheckedRadioButtonId()){
                case R.id.radioButton_munch:
                    tempSize="Personal Pan";
                    SPrice=5;
                    break;
                case R.id.radioButton_hungry:
                    tempSize="Regular Pan";
                    SPrice=9;
                    break;
                case R.id.radioButton_horse:
                    tempSize="Large Pan";
                    SPrice=12;
                    break;
            }
            Size.setText(tempSize);
            SizePrice = SPrice;
        }
    });

checkBoxListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Topping = (TextView)findViewById(R.id.textView_TopAns);
            Topping.setText(" ");

            if(ChkCheese.isChecked()){
                Topping.setText(Topping.getText().toString() + ChkCheese.getText().toString());
                ToppingPrice=2.5;
            }
            if(ChkMushroom.isChecked()){
                Topping.setText(Topping.getText().toString() + "," + ChkMushroom.getText().toString());
                ToppingPrice=1;
            }

        };
        ChkCheese.setOnClickListener(checkBoxListener);
        ChkMushroom.setOnClickListener(checkBoxListener);

        TotalPrice = SizePrice + CrustPrice + ToppingPrice;
        Price.setText("$" + TotalPrice );
    }
}

1 个答案:

答案 0 :(得分:0)

输入此代码:

TotalPrice = SizePrice + CrustPrice + ToppingPrice;
Price.setText("$" + TotalPrice );

onClick()方法内:

    @Override
    public void onClick(View v) {
        Topping = (TextView)findViewById(R.id.textView_TopAns);
        Topping.setText(" ");

        if(ChkCheese.isChecked()){
            Topping.setText(Topping.getText().toString() + ChkCheese.getText().toString());
            ToppingPrice=2.5;
        }
        if(ChkMushroom.isChecked()){
            Topping.setText(Topping.getText().toString() + "," + ChkMushroom.getText().toString());
            ToppingPrice=1;
        }
        TotalPrice = SizePrice + CrustPrice + ToppingPrice;
        Price.setText("$" + TotalPrice );   

    };

因此它会在每次单击复选框时执行。