This Notes is according to the udacity free course :
Android Development for Beginners
Lesson 2A : Make the Quantity Picker Work
In Last Class we still cannot fix the problem to increase the coffee quantity automatic.
After the practice, now we can face the problem.
In this class, the most important concept is Global variable.
Keep it in your mind, it is the key word to fix the problem.
We use one pic to explain what the problem we face on.
We can see that "int quantity = 2" is exist in different code sequence with a complete brackets, they execute the action individually.
And Now, what we have to do is to level up the code " int quantity = 2" to a public class.
Tips : When you create a global variable the words will turn into purple. If it is a local variable, it will shows up in black.
H.W.
The Code is create a GLOBAL VARIABLE and the "int quantity = 2" is under " public class MainActivity.
After the procedure, you can update the coffee quantity immediately.
Press Run and check out your application.
Android Development for Beginners
Lesson 2A : Make the Quantity Picker Work
In Last Class we still cannot fix the problem to increase the coffee quantity automatic.
After the practice, now we can face the problem.
In this class, the most important concept is Global variable.
Keep it in your mind, it is the key word to fix the problem.
We use one pic to explain what the problem we face on.
We can see that "int quantity = 2" is exist in different code sequence with a complete brackets, they execute the action individually.
And Now, what we have to do is to level up the code " int quantity = 2" to a public class.
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
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int quantity = 2; | |
@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) { | |
int numberOfCoffees = 2; | |
display(numberOfCoffees); | |
displayPrice(numberOfCoffees * 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) { | |
int quantity = 2; | |
quantity = 1; | |
display(quantity); | |
} |
Tips : When you create a global variable the words will turn into purple. If it is a local variable, it will shows up in black.
H.W.
The Code is create a GLOBAL VARIABLE and the "int quantity = 2" is under " public class MainActivity.
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
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int quantity = 2; | |
@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) { | |
int numberOfCoffees = 2; | |
display(numberOfCoffees); | |
displayPrice(numberOfCoffees * 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); | |
} |
After the procedure, you can update the coffee quantity immediately.
Press Run and check out your application.
留言
張貼留言