Step 05 - Research + New Control Layout

    We need radio buttons, some sort of selection/combo box + a progress bar. These can be found in various locations in the pallette:

    Step 05 - 图2

    … and we can bring in three fields into the class:

    1. private ProgressBar progressBar;
    2. private NumberPicker amountPicker;

    We can also open up three pages of documentation - which we can reverse engineer from the package/class names:

    Note this time we have gone to the Activity class before actually creating the controls. We should do this now - and remember to use the same names (for the IDs) as we create the controls.

    Getting the layout +id names as shown above may take some practice. However, it is an essential skill to get on top of, even it it takes a lot of trial and error.

    For reference purposes (try to do it yourself first!), these are the relevant generated xml files:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3. <string name="app_name">Donation</string>
    4. <string name="action_settings">Settings</string>
    5. <string name="donateTitle">Welcome Homer</string>
    6. <string name="donateSubtitle">Please give generously</string>
    7. <string name="donateButton">Button</string>
    8. <string name="PayPal">PayPal</string>
    9. <string name="Direct">Direct</string>
    10. </resources>

    If we have our naming conventions right - then we can bind to these new controls in onCreate:

    1. package com.example.donation;
    2. import android.os.Bundle;
    3. import android.util.Log;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.RadioGroup;
    7. import android.widget.NumberPicker;
    8. import android.widget.ProgressBar;
    9. public class Donate extends Activity
    10. {
    11. private Button donateButton;
    12. private RadioGroup paymentMethod;
    13. private ProgressBar progressBar;
    14. private NumberPicker amountPicker;
    15. @Override
    16. protected void onCreate(Bundle savedInstanceState)
    17. {
    18. super.onCreate(savedInstanceState);
    19. setContentView(R.layout.activity_donate);
    20. donateButton = (Button) findViewById(R.id.donateButton);
    21. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
    22. progressBar = (ProgressBar) findViewById(R.id.progressBar);
    23. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
    24. amountPicker.setMinValue(0);
    25. amountPicker.setMaxValue(1000);
    26. }
    27. @Override
    28. public boolean onCreateOptionsMenu(Menu menu)
    29. {
    30. getMenuInflater().inflate(R.menu.donate, menu);
    31. return true;
    32. }
    33. public void donateButtonPressed (View view)
    34. {
    35. Log.v("Donate", "Donate Pressed!");
    36. }