2017-06-08 4 views
0

Ich möchte die Bedingung für das Feld Benutzername hinzufügen. Wenn der Benutzer seinen Namen nicht eingegeben hat, sollte eine Nachricht auf dem Bildschirm angezeigt werden "Bitte geben Sie Ihren Namen"Ich möchte Bedingung in meinem Benutzernamen Feld hinzufügen

Wo sollte ich die if-Anweisung in meinem Java-Code?

Kann mir jemand dabei helfen?

Dies ist mein Java-Code unter:

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

Und hier ist mein XML-Code für das Layout

<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> 
+0

Fügen Sie eine Methode hinzu, die die Gültigkeit des Benutzernamens überprüft. Dann binden Sie einen Listener an Ihren 'EditText', der den Benutzernamen jedes Mal bestätigt, wenn ein Zeichen eingegeben wird. Wenn die Überprüfung fehlschlägt, rendern Sie die Fehlermeldung. –

+0

Können Sie etwas mehr erklären, bedeutet, wenn Sie Toast zeigen möchten. –

+0

Ich bin ein Anfänger und ich möchte das mit den Kontrollflussanweisungen, d. H. Der if-Anweisung tun. – mani

Antwort

0

Sie sind bereits die "quantity == 0" in submitOrder() gibt fügen Sie eine weitere eine Bedingung

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

Oder Wenn eine andere Bedingung Sie möchten überprüfen ...

+0

das hat nicht funktioniert, Alter – mani

Verwandte Themen