有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Android Studio中的复选框和按钮更新总价

这家咖啡店出售一种咖啡,售价5美元,还可以选择生奶油和巧克力,每种售价1美元。我不知道如何正确地将CheckBox选项中的鲜奶油和巧克力添加到show_total

例如,一杯加奶油和巧克力的咖啡应该等于7美元,三杯加巧克力的咖啡应该等于18美元

我是否需要调用一个单独的方法来获得OnStreamClick、onChocClick、countIN和countDE,以便为一个总价正常工作

import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.CheckBox;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import java.text.NumberFormat;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

     TextView show_quantity, show_total, show_summary;
     Button order, plus_one, negative_one;
     EditText customer_name;
     CheckBox whipped_cream, chocolate;
     int counter = 0;


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

    show_quantity = (TextView) findViewById(R.id.amount);
    show_total = (TextView) findViewById(R.id.total);
    show_summary = (TextView) findViewById(R.id.summary);
    customer_name = (EditText) findViewById(R.id.customer_name);
    plus_one = (Button) findViewById(R.id.plus_one);
    negative_one = (Button) findViewById(R.id.negative_one);
    whipped_cream = (CheckBox) findViewById(R.id.whipped_cream);
    chocolate = (CheckBox) findViewById(R.id.chocolate);
    order = (Button) findViewById(R.id.order);

    order.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customer_name.getText().toString().isEmpty()) {
                Toast.makeText(getApplicationContext(), "Please enter a    name.", Toast.LENGTH_SHORT).show();
            } else {
                show_summary.setText("ORDER SUMMARY" + " \n" +
                "Customer - " + customer_name.getText().toString() + " \n" +
                "Whipped Cream? "  + whipped_cream.isChecked() + " \n" +
                "Chocolate? " + chocolate.isChecked() + " \n" +
                "Total - " + show_total.getText().toString() + " \n" +
                "Thank you!");
            }
        }
    });
}

public void onCreamClick(View c) {

    boolean ch1 = whipped_cream.isChecked();

    if (!ch1) {
        double coffee = 5;
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String cof = format.format(coffee);
        show_total.setText(cof);}
    else  if (ch1) {
        double cream = 1;
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String crm = format.format(cream);
        show_total.setText(crm);}
}

public void onChocClick(View chc) {

    boolean ch2 = chocolate.isChecked();

    if (!ch2) {
        double coffee = 5;
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String cof = format.format(coffee);
        show_total.setText(cof);}
    else if (ch2){
        double chocolate = 1;
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String choc = format.format(chocolate);
        show_total.setText(choc);}
}

public void countIN(View in) {
    counter++;
    double coffee = 5 * counter;
    if (counter > 10) {
        Toast.makeText(getApplicationContext(), "You can not order more than   10 coffees", Toast.LENGTH_SHORT).show();
    } else {
        show_quantity.setText(Integer.toString(counter));
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String cof = format.format(coffee);
        show_total.setText(cof);
    }
}

public void countDE(View de) {
    counter--;
    double coffee = 5 * counter;
    if (counter < 0) {
        Toast.makeText(getApplicationContext(), "Please order at least one coffee", Toast.LENGTH_SHORT).show();
    } else {
        show_quantity.setText(Integer.toString(counter));
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
        String cof = format.format(coffee);
        show_total.setText(cof);
    }
}

public static double display_total(double price, double total) {
    return price;
}
}

共 (1) 个答案

  1. # 1 楼答案

    您需要在复选框和按钮上注册侦听器,以便对这些操作做出反应

    whipped_cream.setOnCheckedChangeListener((checkBox, isChecked) -> onCreamClick());