2015-07-03 58 views
0

在我開始之前我想說我只是一個初學者,所以我希望你能理解我爲什麼要問這樣一個問題簡單的問題:)如何在增加/減少int值後將int值重置爲0

隨着「+」和「 - 」按鈕,我調整數量。然後用「訂單」按鈕,我顯示價格。但是當我按下「重新啓動」按鈕並再次調整數量時,數量不會從0開始,而是從最新數量開始。

這是XML:

<RelativeLayout 
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"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:textSize="16sp" 
    android:textAllCaps="true" 
    android:text="Quantity" 
    android:id="@+id/quantity" /> 

<Button 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/quantity" 
    android:text="+" 
    android:id="@+id/increment" 
    android:onClick="increment" /> 

<TextView 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/quantity" 
    android:layout_toRightOf = "@id/increment" 
    android:textSize="16sp" 
    android:textColor="#000000" 
    android:gravity="center" 
    android:text="0" 
    android:id="@+id/quantity_text_view" /> 

<Button 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/quantity" 
    android:layout_toRightOf= "@id/quantity_text_view" 
    android:text="-" 
    android:id="@+id/decrement" 
    android:onClick="decrement" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/increment" 
    android:textSize="16sp" 
    android:textAllCaps="true" 
    android:text="Price" 
    android:id="@+id/price" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/price" 
    android:textSize="16sp" 
    android:textColor="#000000" 
    android:text="$0.00" 
    android:id="@+id/price_text_view" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_below= "@id/price_text_view" 
    android:text="Order" 
    android:id="@+id/button" 
    android:onClick="submitOrder" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below= "@id/button" 
    android:textSize="16sp" 
    android:text="" 
    android:id="@+id/enjoy" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="16dp" 
    android:layout_marginBottom="16dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="Restart" 
    android:id="@+id/restart" 
    android:onClick="restart" /> 

</RelativeLayout> 

這裏是在Java:

public class MainActivity extends ActionBarActivity { 

int quantity = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      TextView view = (TextView) findViewById(R.id.enjoy); 
      view.setPadding(16, 0, 0, 16); 
      view.setText("Enjoy!"); 
      displayPrice(quantity * 5); 
     } 
    }); 
} 

/** 
* This method is called when the + button is clicked. 
*/ 
public void increment(View view){ 
    quantity = quantity + 1; 
    display(quantity); 
} 

/** 
* This method is called when the - button is clicked. 
*/ 
public void decrement(View view){ 
    quantity = quantity - 1; 
    display(quantity); 
} 

/** 
* This method is called when the order button is clicked. 
*/ 
public void submitOrder(View view) { 
    display(quantity); 

} 

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

/** 
* This method displays the given price on the screen. 
*/ 
private void displayPrice(int number) { 
    TextView priceTextView = (TextView) findViewById(R.id.price_text_view); 
    priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); 
} 

public void restart(View v) { 
    int quantity = 0; 
    display(quantity); 
    displayPrice(quantity); 
    TextView view = (TextView) findViewById(R.id.enjoy); 
    view.setPadding(0, 0, 0, 0); 
    view.setText(""); 
} 
} 

我想實現的是按下重啓鍵,值了之後int quantity將重置爲0.

+0

你覺得'int quantity'是什麼意思? – SLaks

+0

'int quantity = 0;' - >'quantity = 0'這叫做變量陰影。 https://en.wikipedia.org/wiki/Variable_shadowing – njzk2

回答

0

在您正在創建的重啓方法中一個名爲quantity的新變量;但是,您應該使用已經爲班級定義的數量。只需將方法更改爲:

public void restart(View v) { 
    quantity = 0; 
    display(quantity); 
    displayPrice(quantity); 
    TextView view = (TextView) findViewById(R.id.enjoy); 
    view.setPadding(0, 0, 0, 0); 
    view.setText(""); 
} 
0

變量墊子的範圍。您已在重新啓動方法內聲明瞭一個新變量,而不是使用已在MainActivity中聲明的變量。

public void restart(View v) { 
    quantity = 0; 
    display(quantity); 
    displayPrice(quantity); 
    TextView view = (TextView) findViewById(R.id.enjoy); 
    view.setPadding(0, 0, 0, 0); 
    view.setText(""); 
} 
+0

Thx,它的工作! –