Java 要在我的用户名字段上添加条件吗

Java 要在我的用户名字段上添加条件吗,java,android,Java,Android,我想在用户名字段中添加条件。 如果用户没有输入姓名,屏幕上会显示一条敬酒信息“请输入您的姓名” 我应该把if语句放在java代码的哪里 有人能帮我吗 下面是我的java代码: package com.infinitystone.mani.justjava; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widg

我想在用户名字段中添加条件。 如果用户没有输入姓名,屏幕上会显示一条敬酒信息“请输入您的姓名”

我应该把if语句放在java代码的哪里

有人能帮我吗

下面是我的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);
    }
}
包com.infinitystone.mani.justjava;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*此应用程序显示订单以订购咖啡。
*/
公共类MainActivity扩展了AppCompatActivity{
整数数量=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
*单击+按钮时调用此方法。
*/
公共空间增量(视图){
如果(数量>=10){
Toast.makeText(getApplicationContext(),“每次订单仅10杯”,
吐司。长度(短)。show();
返回;
}
数量=数量+1;
显示数量(数量);
}
/**
*单击-按钮时调用此方法。
*/
公共无效减量(视图){

如果(数量您已经在
submitor()中验证
“数量==0”
,则添加另一个条件

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

如果您需要检查任何其他条件…

请添加一个检查用户名有效性的方法。然后将侦听器绑定到您的
EditText
,每次输入字符时该方法都会验证用户名。如果验证失败,请呈现错误消息。您能再解释一下吗,意思是当您想要显示toast时。我是初学者,我是nt使用控制流语句(即if语句)执行此操作。
if (username.getText().equals("")) {
  Toast
    .makeText(
      mContext,
      "Stay tuned with us we'll bring more exitting features for you!",
      Toast.LENGTH_SHORT
    )
    .show();
}