1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.example.android.justjava; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends ActionBarActivity { int quantity = 2 ; @Override protected void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void increment(View view){ quantity = quantity + 1 ; display(quantity); } public void decrement(View view){ quantity = quantity - 1 ; display(quantity); } public void submitOrder(View view){ displayPrice(quantity * 5 ); } private void display( int number){ TextView quantityTextView = (TextView) findViewById( R.id.quantity_text_view); quantityTextView.setText( "" + number); } private void displayPrice( int number){ TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical" android:paddingLeft = "@dimen/activity_horizontal" android:paddingRight = "@dimen/activity_horizontal" android:paddingTop = "@dimen/activity_vertical" android:orientation = "vertical" tools:context = ".MainActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginBottom = "16dp" android:text = "Quantity" android:textAllCaps = "true" /> < Button android:layout_height = "48dp" android:layout_width = "48dp" android:text = "+" android:onClick = "increment" /> < TextView android:id = "@+id/quantity_text_view" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "0" android:textSize = "16sp" adnroid:textColor = "@android:color/black" /> < Button android:layout_height = "48dp" android:layout_width = "48dp" android:text = "-" android:onClick = "decrement" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:text = "Price" android:textAllCaps = "true" /> < TextView android:id = "@+id/price_text_view" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:text = "$0" android:textSize = "16sp" adnroid:textColor = "@android:color/black" /> < Button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginTop = "16dp" android:text = "Order" android:onClick = "submitOrder" /> </ LinearLayout > |