- Hands-On Android UI Development
- Jason Morris
- 626字
- 2021-07-02 23:26:09
Wiring the CaptureClaimActivity events
Now that we have a way for the user to pick a date for their travel expense claims, we need to actually wire it into the CaptureClaimActivity, which is where all the logic and wiring for the screen will live. To start wiring the events for the CaptureClaimActivity, follow these steps:
- Open the CaptureClaimActivity.java file in Android Studio.
- Now, declare a new field in the class (before the onCreate method) for the DatePickerWrapper that you wrote (Android Studio can help by writing the import for you):
private DatePickerWrapper selectedDate;
- You'll note that (by default) the FloatingActionButton object is wired up with a simple anonymous event handler that will look something like this:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(
view,
"Replace with your own action",
Snackbar.LENGTH_LONG
).setAction("Action", null).show();
}
});
- This is how many once off events get wired up (as discussed earlier in this chapter), but it's not what we want to do here, so remove that entire block of code.
- At the end of the onCreate method instantiate the DatePickerWrapper object by searching for the date TextView you added to the layout:
selectedDate = new DatePickerWrapper((TextView) findViewById(R.id.date));
- You don't need to hold any other references to the date TextView, since you'll only ever need to access it through the DatePickerWrapper class. Try running your application now, and see how the date picker works.
In the application, you'll note that you can select the category icons, and they will work exactly as expected. However, the label following them isn't wired up at all, and no labels will be displayed, leaving the user confused as to what they are actually selecting. To fix this, you'll need another event listener that sets the content of the label when the state of the RadioButton widgets is changed. This is another case where a specialized listener class will make sense; since it'll be usable anytime, you have a group of icon RadioButton widgets and a single label for all of them:
- Right-click on the ui package and select New | Java Class.
- Name the new class as IconPickerWrapper.
- Add android.widget.RadioGroup.OnCheckedChangeListener to the interfaces box.
- Create a field for the TextView label, and a constructor to capture it:
private final TextView label;
public IconPickerWrapper(final TextView label) {
this.label = label;
}
- Add a method to set the label text content:
public void setLabelText(final CharSequence text) {
label.setText(text);
}
- Complete the onCheckedChange method to set the label text from the contentDescription field of the selected RadioButton:
@Override
public void onCheckedChanged(
final RadioGroup group,
final int checkedId) {
final View selected = group.findViewById(checkedId);
setLabelText(view.getContentDescription());
}
This is a very straightforward class, but it also potentially serves other purposes in your application, and it only makes two assumptions about the RadioGroup it will be connected to:
- Every RadioButton has a valid ID
- Every RadioButton has a contentDescription that will serve as a text label
Going back to CaptureClaimActivity, you'll want to wire this new listener into the layout through the following steps:
- Before the onCreate method, create a new field to keep track of the RadioGroup, where the user can select the category icon:
private RadioGroup categories;
- Then, at the end of the onCreate method, you'll need to find this RadioGroup in the layout, and instantiate its event handler:
categories = (RadioGroup) findViewById(R.id.categories);
categories.setOnCheckedChangeListener(
new IconPickerWrapper(
(TextView) findViewById(R.id.selected_category)
)
);
- Finally, set the default selection to other; this action will also trigger the event handler before the screen is presented to the user. This means the label will also be populated when the user first sees the Capture Claim screen:
categories.check(R.id.other);
Now if you run the application again, you'll see the labels defined to appear beneath the selected icons as you toggle through the category icons.