在增加/减少其值后​​,如何将int的值重置为0

时间:2015-07-03 16:49:06

标签: java android xml int

在开始之前,我想说我只是一个初学者,所以我希望你能理解为什么我会问这么简单的问题:)

使用“+”和“ - ”按钮,我调整数量。然后用“订单”按钮,我显示价格。但是,当我按下“重新启动”按钮然后再次调整数量时,数量不会从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.

2 个答案:

答案 0 :(得分: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("");
}

答案 1 :(得分:0)

变量垫的范围。您已在restart方法中声明了一个新变量,而是使用您已在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("");
}