Android development-RadioGroup and RadioButton controls
RadioButton is a single-choice button, which provides a"Select one more"Operation mode is a commonly used component in Android development. For example, when a user is registered, only one of the" male "or" female "can be selected for gender selection. Unlike Web development, you can use RadioGroup in Android to define radio button components.
RadioGroupThe class definition is shown in:
RadioGroup only provides the RadioButton single-choice button container. You can add multiple RadioButton containers to use this container. To set the content of the single-choice button, you need to use the RadioButton class.
RadioButtonThe class definition is shown in:
java.lang.Object
Therefore, this component is similar to the Button component. The difference is that the defined RadioButton component must be placed in the RadioGroup component.
-----------------------------------------------------------------------------------------------------
Public method of RadioGroup
Public void addView (View child, int index, ViewGroup. LayoutParams params)
Add a subview using the specified layout Parameter
Parameters
Child View
Position of the subview to be added to the index
Layout parameters of the Child view to be added for params
Public void check (int id)
If-1 is passed as the specified selection identifier to clear the check status of the radio button group, it is equivalent to calling the clearCheck () operation.
Parameters
Id: the unique identifier (id) of the radio button to be selected in this group)
See
GetCheckedRadioButtonId ()
ClearCheck ()
Public void clearCheck ()
The current selection status is cleared. When the selection status is cleared, all the radio buttons in the radio button group are deselected. getCheckedRadioButtonId () returns null.
See
Check (int)
GetCheckedRadioButtonId ()
Public RadioGroup. LayoutParams generateLayoutParams (AttributeSet attrs)
Returns a new layout parameter set based on the provided property set.
Parameters
Attrs is used to generate attributes of layout parameters.
Return Value
Returns an instance of ViewGroup. LayoutParams or its subclass.
Public int getCheckedRadioButtonId ()
Returns the ID of the selected radio button in the single-choice button group. If it is not selected,-1 is returned.
Return Value
Returns the ID of the selected radio button in the radio button group.
See
Check (int)
ClearCheck ()
Public void setOnCheckedChangeListener (RadioGroup. OnCheckedChangeListener listener)
Register a callback function to be called when the status of the selected button in this radio button group changes
Parameters
Listener: the callback function to be called when the status of the single-choice button is changed
Public void setOnHierarchyChangeListener (ViewGroup. OnHierarchyChangeListener listener)
Register a callback function to be called when the sub-content is added to or removed from the View
Parameters
The callback function to be called when the level of the listener changes
Protected Methods
Protected LinearLayout. LayoutParams generatedefalalayoutparams ()
When the layout is in the vertical direction, a set of layout parameters whose width is "fill parent element" (MATCH_PARENT) and height is "Package content" will be returned. If the layout is in the horizontal direction, returns a set of layout parameters whose width is "Package content" and whose height is "fill parent element ".
(Match_parent is fill_parent, public static final int FILL_PARENT/MATCH_PARENT =-1)
Return Value
Returns a set of default layout parameters.
Protected void onFinishInflate ()
This method is called when the view is loaded from XML and the corresponding sub-view is added,
Even if the subclass overrides this method, make sure to call the method of the parent class (usually in the first sentence of the method) to complete the corresponding call parameters.
Return Value
Returns a set of default layout parameters.
------------------------------------------------------------------------------------------------------
The Layout file code is as follows:
Use in RadioGroup
android:orientation="horizontal"
To set the internal arrangement of RadioButton, horizontal is horizontal, vertical is vertical.
android:checkedButton="@+id/male"
Set the default RadioButton in the RadioGroup.
You can perform event processing on the single-choice button RadioGroup. When you select an option, the listener is triggered to process the event. The registered event listener is OnCheckedChangeListener. The method is as follows:
public void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)
The Event code is as follows:
/** RadioGroup and RadioButton **/sexRadioGroup = (RadioGroup) findViewById (R. id. rgSex); male = (RadioButton) findViewById (R. id. male); female = (RadioButton) findViewById (R. id. female); sexRadioGroup. setOnCheckedChangeListener (new RadioGroup. onCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {String msg = ""; if (male. getId () = checkedId) {msg = "the selected gender is:" + male. getText (). toString ();} if (female. getId () = checkedId) {msg = "the selected gender is:" + female. getText (). toString ();} Toast. makeText (getApplicationContext (), msg, Toast. LENGTH_LONG ). show ();}});
Shows the effect: