想要在我的用户名字段中添加条件

时间:2017-06-08 10:03:46

标签: java android

我想在用户名字段中添加条件。 如果用户没有输入他的名字,屏幕上应显示一个Toast消息"请输入你的姓名"

我应该在我的java代码中将if语句放在哪里?

有人可以帮我吗?

这是我的java代码:

package com.infinitystone.mani.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {
    int quantity = 0;

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

    /**
     * This method is called when the + button is clicked.
     */

    public void increment(View view) {
        if (quantity >= 10){
            Toast.makeText(getApplicationContext(), "Only 10 cups per order",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity + 1;
        displayQuantity(quantity);
    }

    /**
     * This method is called when the - button is clicked.
     */

    public void decrement(View view) {
        if (quantity<=0){
            Toast.makeText(this, "Dude! are you drunk ?",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity - 1;
        displayQuantity(quantity);
    }

    /**
     * This method is called when the order button is clicked.
     */

    public void submitOrder(View view) {
        if (quantity == 0){
            Toast.makeText(this, "How many bottles you had ?",
                    Toast.LENGTH_SHORT).show();
            return;
        }
            EditText userName = (EditText) findViewById(R.id.name_edit_text);
            String name = userName.getText().toString();
            CheckBox whippedCream = (CheckBox) findViewById(R.id.checkbox_cream);
            boolean hasWhippedCream = whippedCream.isChecked();
            CheckBox chocolate = (CheckBox) findViewById(R.id.checkbox_chocolate);
            boolean hasChocolate = chocolate.isChecked();
            int finalPrice = calculatePrice(hasWhippedCream, hasChocolate);
            String priceMessage = createOrderSummary(finalPrice, hasWhippedCream, hasChocolate, name);
            displayMessage(priceMessage);
    }

    /**
     * Calculates the price of the order.
     *
     * @param addWhippedCream tells if the user wants to add whipped cream
     * @param addChocolate tells if the user wants to add chocolate
     * @return total price of the order
     */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
        int basePrice = 5;
        if (addWhippedCream){
            basePrice = basePrice + 1;
        }

        if (addChocolate){
            basePrice = basePrice +2;
        }
        return quantity * basePrice;
    }

    /**
     * Creates the summary of the order
     *
     * @param price
     * @param addWhippedCream
     * @param addChocolate
     * @param addUserName
     * @return
     */
    private String createOrderSummary(int price,
                                      boolean addWhippedCream,
                                      boolean addChocolate,
                                      String addUserName) {
        String orderSummary = "Name: " + addUserName;
        orderSummary += "\nQuantity: " + quantity + " cups";
        orderSummary += "\nWhipped cream added: " + addWhippedCream;
        orderSummary += "\nChocolate added: " + addChocolate;
        orderSummary += "\nTotal = $" + price;
        orderSummary += "\nThank You!";
        return orderSummary;
    }

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

    /**
     * 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);
    }
}

这是布局的XML代码

<ScrollView 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:hint="Name"
            android:inputType="textCapWords" />

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

        <CheckBox
            android:id="@+id/checkbox_cream"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="18dp"
            android:text="Whipped Cream"
            android:textSize="16sp" />

        <CheckBox
            android:id="@+id/checkbox_chocolate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="18dp"
            android:text="Chocolate"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="168dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/darker_gray">

        </View>

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            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="8dp"
                android:layout_marginRight="8dp"
                android:text="0"
                android:textColor="@android:color/black"
                android:textSize="16sp" />

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

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="168dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/darker_gray">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:text="order summary"
            android:textAllCaps="true" />

        <TextView
            android:id="@+id/order_summary_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:text="$0"
            android:textColor="@android:color/black"
            android:textSize="16sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:onClick="submitOrder"
            android:text="Order" />

    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

您已经在"quantity == 0"验证了submitOrder(),还有另外一个条件

if (username.getText().equals("")) {
  Toast
    .makeText(
      mContext,
      "Stay tuned with us we'll bring more exitting features for you!",
      Toast.LENGTH_SHORT
    )
    .show();
}

或者 如果您想要检查任何其他条件......