samedi 23 décembre 2017

Defining a checkbox in Android Studio

I'm new coding and have started an online course for android basics. I've been trying to add a checkbox and I can't define it in the main code, only inside one of my methods - I was just wondering why that is. Thanks very much in advance!!

//Displays the message
private void displayMessage(String message) {
    TextView orderSummaryTexView = (TextView) findViewById(R.id.order_summary_text_view);
    orderSummaryTexView.setText(message);
}

//Calculates the price
private int calculatePrice() {
    int price = 5;
    return (price * quantity);



//Prepares the final message for printing
    private String createOrderSummary(int cost, boolean cream) {
    String genMessage = ("\nQuantity: " + quantity + "\n Has Cream: " + cream + "\nTotal: " + cost + "$ \nThank you!" );
    return (genMessage);
}

//Prints out the final result
  public void submitOrder(View view) {
    CheckBox CreamCheckBox = (CheckBox) findViewById(R.id.Cream);
    boolean creamy = CreamCheckBox.isChecked();
    int price = calculatePrice();
    displayMessage(createOrderSummary(price, creamy));
}

Unless I defined the CheckBox inside my SubmitOrder method - the app would crash.




Aucun commentaire:

Enregistrer un commentaire