不知道如何调用复选框的情况

时间:2016-07-06 16:10:04

标签: java android checkbox

我是Android开发新手,我参加Udacity.com的初学者免费课程...我无法通过这项任务: 根据复选框点击,在摘要顺序中添加一个新行 The final result is to show "Add whipped cream? True" if the user clicked the whipped cream checkbox as appear in this image :

需要您的帮助才能使此复选框正常工作,您的回复将会受到赞赏,因为我陷入了这种搜索并且没有任何进展。

注意:这是我第一次创建复选框。

这里是(.java)和(.xml)代码:

package com.example.android.justjava;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends ActionBarActivity {

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

    int quantity = 0;

    /**
     * This method is called when the + button is clicked.
     */
    public void increment(View view) {
        quantity = quantity + 1;
        displayQuantity(quantity);
    }

    /**
     * This method is called when the - button is clicked.
     */
    public void decrement(View view) {
        quantity = quantity - 1;
        displayQuantity(quantity);
    }

    /**
     * When the checkbox is clicked
     * if checked then display message "true" in the order summary
     * if not checked then display message "false" in the order summary
     */
    public void onCheckboxClicked(View view) {
        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.hasWhippedCream);
        boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
        switch (view.getId()) {
            case R.id.hasWhippedCream:
                if (hasWhippedCream)
                    //With whipped cream
                    displayMessage("true");
                else
                    // Without whipped cream
                    displayMessage("false");
                break;
        }
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        // Calculate the price with
        int price = calculatePrice();

        // String message
        displayMessage(createOrderSummary(price, b);
    }

    /**
     * Calculates the price of the order
     *
     * @return the total price of coffees
     */
    public int calculatePrice() {
        return quantity * 5;
    }

    /**
     * This method is to create order summary
     *
     * @param price return text summary as a string message
     */
    private String createOrderSummary(int price, boolean hasWhippedCream) {
        String priceMessage = "ORDER SUMMARY";
        priceMessage += "\nName: Momen Ahmed";
        priceMessage += "\nQuantity: " + quantity;
        priceMessage += "\nAdd whipped cream " + hasWhippedCream;
        priceMessage += "\nTotal: $" + price;
        priceMessage += "\nThank You";
        return priceMessage;
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int numberOfCoffees) {
        TextView quantityTextView = (TextView) findViewById(
                R.id.quantity_text_view);
        quantityTextView.setText("" + numberOfCoffees);
    }


    /**
     * This method displays the given price on the screen.
     */
    //////private void displayPrice(int number) {
    /////TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
    //////priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    ///////}

    /**
     * This method displays the given text on the screen.
     * <p/>
     * Variable assignment to the textview in XML file as
     * Variable data type Variable name = Value
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
        orderSummaryTextView.setText(message);
        orderSummaryTextView.setTextColor(Color.RED);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="toppings"
        android:textAllCaps="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:text="Whipped cream"
        android:id="@+id/hasWhippedCream"
        android:onClick="onCheckboxClicked"
        android:textSize="16sp"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="16dp"
        android:text="Quantity"
        android:textAllCaps="true" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="decrement"
            android:text="-" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:text="0"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="16sp" />

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="increment"
            android:text="+" />

    </LinearLayout>

    <TextView
        android:id="@+id/order_summary_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="16dp"
        android:text=""
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text=""
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="submitOrder"
        android:text="order" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

package com.example.android.justjava;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends ActionBarActivity {

CheckBox whippedCreamCheckBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        whippedCreamCheckBox = (CheckBox) findViewById(R.id.hasWhippedCream);
    }

    int quantity = 0;

    /**
     * This method is called when the + button is clicked.
     */
    public void increment(View view) {
        quantity = quantity + 1;
        displayQuantity(quantity);
    }

    /**
     * This method is called when the - button is clicked.
     */
    public void decrement(View view) {
        quantity = quantity - 1;
        displayQuantity(quantity);
    }

    /**
     * When the checkbox is clicked
     * if checked then display message "true" in the order summary
     * if not checked then display message "false" in the order summary
     */
    public void onCheckboxClicked(View view) {

        boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
        switch (view.getId()) {
            case R.id.hasWhippedCream:
                if (hasWhippedCream)
                    //With whipped cream
                    displayMessage("true");
                else
                    // Without whipped cream
                    displayMessage("false");
                break;
        }
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        // Calculate the price with
        int price = calculatePrice();

        boolean hasWhippedCream = whippedCreamCheckBox.isChecked();

        // String message
        displayMessage(createOrderSummary(price, hasWhippedCream));
    }

    /**
     * Calculates the price of the order
     *
     * @return the total price of coffees
     */
    public int calculatePrice() {
        return quantity * 5;
    }

    /**
     * This method is to create order summary
     *
     * @param price return text summary as a string message
     */
    private String createOrderSummary(int price, boolean hasWhippedCream) {
        String priceMessage = "ORDER SUMMARY";
        priceMessage += "\nName: Momen Ahmed";
        priceMessage += "\nQuantity: " + quantity;
        priceMessage += "\nAdd whipped cream " + hasWhippedCream;
        priceMessage += "\nTotal: $" + price;
        priceMessage += "\nThank You";
        return priceMessage;
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int numberOfCoffees) {
        TextView quantityTextView = (TextView) findViewById(
                R.id.quantity_text_view);
        quantityTextView.setText("" + numberOfCoffees);
    }


    /**
     * This method displays the given price on the screen.
     */
    //////private void displayPrice(int number) {
    /////TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
    //////priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    ///////}

    /**
     * This method displays the given text on the screen.
     * <p/>
     * Variable assignment to the textview in XML file as
     * Variable data type Variable name = Value
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
        orderSummaryTextView.setText(message);
        orderSummaryTextView.setTextColor(Color.RED);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="toppings"
        android:textAllCaps="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:text="Whipped cream"
        android:id="@+id/hasWhippedCream"
        android:onClick="onCheckboxClicked"
        android:textSize="16sp"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="16dp"
        android:text="Quantity"
        android:textAllCaps="true" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="decrement"
            android:text="-" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:text="0"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="16sp" />

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="increment"
            android:text="+" />

    </LinearLayout>

    <TextView
        android:id="@+id/order_summary_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="16dp"
        android:text=""
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text=""
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="submitOrder"
        android:text="order" />

</LinearLayout>