mardi 31 janvier 2017

Not sure how to use switch method

I'm trying to make the app add a topping only if the box is checked. I was following a lesson and they showed me how to display the String with the true or false statement next to it. I don't want that. I actually want the string to appear only if the checkbox is checked. Thank you.

Hi this is the XML:

   <EditText
       android:id="@+id/edit_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="Enter your Name"
       android:layout_margin="16dp"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chocolateCheckBox"
    android:text="Add Chocolate On Your Coffee"
    android:layout_margin="16dp"
    android:layout_below="@id/edit_text"
    />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/creamCheckBox"
    android:text="Add Cream On Your Coffee"
    android:layout_marginLeft="16dp"
    android:layout_marginBottom="8dp"
    android:layout_below="@id/chocolateCheckBox"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="QUANTITY"
    android:id="@+id/quantity"
    android:textSize="16sp"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_below="@id/creamCheckBox"
    />


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="16dp"
    android:id="@+id/adding_layout"
    android:layout_below="@id/quantity">

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="-"
        android:onClick="decrement"
        android:id="@+id/minus_button"
        android:width="48dp"
        android:height="48dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/quantity_text_view"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"/>
    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="+"
        android:onClick="increment"
        android:id="@+id/plus_button"/>
</LinearLayout>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/order_summary"
    android:text="ORDER SUMMARY"
    android:textSize="16sp"
    android:layout_below="@id/adding_layout"
    android:layout_marginLeft="16dp"/>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:id="@+id/order_summary_text_view"
    android:textSize="16sp"
    android:layout_margin="16dp"
    android:layout_below="@id/order_summary"
    android:textColor="#000000"/>


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/order_button"
    android:text="ORDER"
    android:layout_marginLeft="16dp"
    android:layout_below="@id/order_summary_text_view"
    android:onClick="submitOrder"/>

  </RelativeLayout>
  </ScrollView>

and this is the java code:

public class MainActivity extends AppCompatActivity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See http://ift.tt/1Shh2Dk for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See http://ift.tt/1Shh2Dk for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
int quantity = 0;

public String submitOrder(View view) {
    CheckBox addChocolate = (CheckBox) findViewById(R.id.chocolateCheckBox);
    boolean hasChocolate = addChocolate.isChecked();

    CheckBox addCream = (CheckBox) findViewById(R.id.creamCheckBox);
    boolean hasCream = addCream.isChecked();


    if (hasChocolate = true){
        String chocolate = "Add Chocolate to the coffee";
        return chocolate;
    }
    if (addCream.isChecked()){
        String cream = "Add Cream to the coffee";
        return cream;
    }


    EditText enterName = (EditText) findViewById(R.id.edit_text);
    Editable addName = enterName.getText();

    int price = calculatePrice();
    String priceMessage = createOrderSummary(addName, price, chocolate,        cream);
    displayMessage(priceMessage);
    return priceMessage;
}

/**  Calculates the price of the order.  */
private int calculatePrice() {

    return quantity * 5;
}


/** displays the number of coffee between the + and - buttons */
private void displayQuantity(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}
private String createOrderSummary(Editable enterName, int calculatePrice, boolean chocolate, boolean cream) {
    String priceMessage = "Name = " + enterName + "\nQuantity : " + quantity + "\n" + chocolate + "\n" + cream + "\nTotal: £ " + calculatePrice + "\nThank you!";
    return priceMessage;

}

public void increment(View view) {
    quantity = quantity + 1;
    displayQuantity(quantity);
}

public void decrement(View view) {
    quantity = quantity - 1;
    displayQuantity(quantity);
}




/**
 * This method displays the given text on the screen.
 */
private void displayMessage(String message) {
    TextView orderSummaryTextView = (TextView)   findViewById(R.id.order_summary_text_view);
    orderSummaryTextView.setText(message);
}

Aucun commentaire:

Enregistrer un commentaire