This Notes is according to the udacity free course :
Android Development for Beginners
Lesson 2A : Fix the Order Button
Finally, you can add new coffee by just click the "+" button, but the order button is still out of work.
Use the Knowledge you learn before from this class to figure it out that how to let it calculating right price.
After the observation, try your best to correct the code.
Tips: Be careful when you set up the global variable "int quantity = 2" in MainActivity.java , it cannot match with the number we write price android:text="0" in activity_main.xml price's TextView.
They should be matched or will cause some error.
The correct code is write here below.
1. activity_main.xml
2. MainActivity.java
Android Development for Beginners
Lesson 2A : Fix the Order Button
Finally, you can add new coffee by just click the "+" button, but the order button is still out of work.
Use the Knowledge you learn before from this class to figure it out that how to let it calculating right price.
After the observation, try your best to correct the code.
Tips: Be careful when you set up the global variable "int quantity = 2" in MainActivity.java , it cannot match with the number we write price android:text="0" in activity_main.xml price's TextView.
They should be matched or will cause some error.
The correct code is write here below.
1. activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout | |
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" | |
android:orientation="vertical" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="Quantity" | |
android:textSize="20sp" | |
android:textAllCaps="true" /> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:text="+" | |
android:layout_marginTop="20dp" | |
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="20sp" | |
android:layout_marginBottom="16dp" | |
android:textColor="@android:color/black"/> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:text="-" | |
android:layout_marginBottom="20dp" | |
android:onClick="decrement" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="Price" | |
android:textSize="20sp" | |
android:textAllCaps="true" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/price_text_view" | |
android:text="0" | |
android:textSize="20sp" | |
android:textColor="@android:color/black"/> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="submitOrder" | |
android:text="Order" | |
android:layout_marginTop="16dp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.justjava; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.text.NumberFormat; | |
/** | |
* 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 order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
displayPrice(quantity * 5); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void increment(View view) { | |
quantity = quantity + 1; | |
display(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void decrement(View view) { | |
quantity = quantity - 1; | |
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)); | |
} | |
} |
留言
張貼留言