Exercises

Archive of lab so far:

Exercises 1:

Consider an alternative to the NumberPicker - specifically one of the “Text Fields” controls:

Exercises - 图1

These are mostly EditView objects:

  • <http://developer.android.com/reference/android/widget/EditText.html>

Redesign the activity to take a value from the picker or directly from a text view:

Exercises - 图2

If the number picker is set to zero, then attempt to get a number from the text view.

Here is a hint (a version of donatButonPressed that does what we want):

1. public void donateButtonPressed (View view)
2. {
3. String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
4. progressBar.setProgress(totalDonated);

6. int donatedAmount =  amountPicker.getValue();
7. if (donatedAmount == 0)
8. {
9. String text = amountText.getText().toString();
10. if (!text.equals(""))
11. donatedAmount = Integer.parseInt(text);
12. }
13. totalDonated  = totalDonated + donatedAmount;
14. Log.v("Donate", amountPicker.getValue() + " donated by " +  method + "\nCurrent total " + totalDonated);
15. }

Exercise 2:

Revise the app such that when the target is achieved (10000) - then no more donations accepted, and the user is made aware of this.

Hint - here is how you can display a simple alert:

1. Toast toast = Toast.makeText(this, "Target Exceeded!", Toast.LENGTH_SHORT);
2. toast.show();

Exercise 3:

Show on screen at all times the total amount donated.

You will use standard TextView for this:

  • <http://developer.android.com/reference/android/widget/TextView.html>

You already have a number of these on screen. Your layout could be revised to look like this:

Exercises - 图3

Archive of lab with Exercises: