Using RadioGroups and RadioButtons, You often use radio buttons when a user should be allowed to only select one item from a small group of items. For instance, a question asking for gender can give three options: male, female, and unspecified. Only one of these options should be checked at a time.The RadioButton objects are similar to CheckBox objects.They have a text label next to them, set via the text attribute, and they have a state (checked or unchecked). However, you can 148 Chapter 7 Exploring User Interface Screen Elements group RadioButton objects inside a RadioGroup that handles enforcing their combined states so that only one RadioButton can be checked at a time. If the user selects a RadioButton that is already checked, it does not become unchecked. However, you can provide the user with an action to clear the state of the entire RadioGroup so that none of the buttons are checked.
Here we have an XML layout resource with a RadioGroup containing four RadioButton objects (shown in Figure 7.7, toward the bottom of the screen).The RadioButton objects have text labels,“Option 1,” and so on.The XML layout resource definition is shown here:
You handle actions on these RadioButton objects through the RadioGroup object.The following example shows registering for clicks on the RadioButton objects within the
RadioGroup:
final RadioGroup group = (RadioGroup)findViewById(R.id.RadioGroup01);
final TextView tv = (TextView)
findViewById(R.id.TextView01);
group.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(
Using Buttons, Check Boxes, and Radio Groups 149
RadioGroup group, int checkedId) {
if (checkedId != -1) {
RadioButton rb = (RadioButton)
findViewById(checkedId);
if (rb != null) {
tv.setText(“You chose: “ + rb.getText());
}
} else {
tv.setText(“Choose 1”);
}
}
});
As this layout example demonstrates, there is nothing special that you need to do to make the RadioGroup and internal RadioButton objects work properly.The preceding code illustrates how to register to receive a notification whenever the RadioButton selection
changes.
The code demonstrates that the notification contains the resource identifier for the specific RadioButton that the user chose, as assigned in the layout file.To do something interesting with this, you need to provide a mapping between this resource identifier (or the text label) to the corresponding functionality in your code. In the example,we query for the button that was selected, get its text, and assign its text to another TextView control that we have on the screen.
As mentioned, the entire RadioGroup can be cleared so that none of the RadioButton objects are selected.The following example demonstrates how to do this in response to a button click outside of the RadioGroup:
final Button clear_choice = (Button) findViewById(R.id.Button01);
clear_choice.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RadioGroup group = (RadioGroup)
findViewById(R.id.RadioGroup01);
if (group != null) {
group.clearCheck();
}
}
}
The action of calling the clearCheck() method triggers a call to the onCheckedChangedListener() callback method.This is why we have to make sure that the resource identifier we received is valid. Right after a call to the clearCheck() method, it is not a valid identifier but instead is set to the value -1 to indicate that no RadioButton is currently checked.
No comments:
Post a Comment