These controls are commonly used, so they are commonly used during operations. Therefore, this part is an important part of Android software development and the content is relatively simple. Introduction to shard types
1. Single-stick operation: A single-stick operation can be seen everywhere in Android. It consists of two parts: radiogroup and radiobutton. A radiogroup contains multiple radiobutton instances. Each radiobutton is a single option, and the radiogroup is controlled. Below is the implementation of XML and code
XML:
- <Radiogroup
- Android: Id = "@ + ID/gendergroup"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: Orientation = "horizontal"
- >
- <Radiobutton
- Android: Id = "@ + ID/femalebutton"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/female"/>
- <Radiobutton
- Android: Id = "@ + ID/malebutton"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/Male"/>
- </Radiogroup>
A simple display of radiogroup and radiobutton is defined above.
The following is the operation code of the event bound to this control:
- // Bind the oncheckedchangelistener of gendergroup to respond to the event
- Gendergroup. setoncheckedchangelistener (NewRadiogroup. oncheckedchangelistener (){
- @ Override
- Public VoidOncheckedchanged (radiogroup group,IntCheckedid ){
- If(Femalebutton. GETID () = checkedid ){
- System. Out. println ("female ");
- Toast. maketext (activity07.This, "Female", Toast. length_short). Show ();
- }Else If(Malebutton. GETID () = checkedid ){
- System. Out. println ("male ");
- Toast. maketext (activity07.This, "Male", Toast. length_short). Show ();
- }
- }
- });
2. the event in the pop-up box (toast) is similar to the joptionpane of swing, but it is called toast. The method used is to add toast where you need the pop-up information. maketext (here is the name of the class object to be popped up. This is the string to be popped up and the length to be popped up ). For more information, see the last line of the above Java code. The dialog box does not need to be configured in XML.
3. check box: the check box does not have the concept of a group as a single sequence, so the operations of the check box are a little more complex than those of a single sequence, because you need to make an Event Response for each check box. The following is an example of a check box.
- <Checkbox
- Android: Id = "@ + ID/swim"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/swim"/>
- <Checkbox
- Android: Id = "@ + ID/run"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/run"/>
- <Checkbox
- Android: Id = "@ + ID/read"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/read"/>
The following is the time response code:
- // The Listener bound to the checkbox is different from the radiogroup method.
- Inclucheckbox. setoncheckedchangelistener (NewCompoundbutton. oncheckedchangelistener (){
- @ Override
- Public VoidOncheckedchanged (compoundbutton buttonview,BooleanIschecked ){
- If(Ischecked ){
- System. Out. println ("swim is checked ");
- }Else{
- System. Out. println ("swim is unchecked ");
- }
- }
- });
- Runcheckbox. setoncheckedchangelistener (NewCompoundbutton. oncheckedchangelistener (){
- @ Override
- Public VoidOncheckedchanged (compoundbutton buttonview,BooleanIschecked ){
- If(Ischecked ){
- System. Out. println ("Run is checked ");
- }Else{
- System. Out. println ("Run is unchecked ");
- }
- }
- });
- Readcheckbox. setoncheckedchangelistener (NewCompoundbutton. oncheckedchangelistener (){
- @ Override
- Public VoidOncheckedchanged (compoundbutton buttonview,BooleanIschecked ){
- If(Ischecked ){
- System. Out. println ("read is checked ");
- }Else{
- System. Out. println ("read is unchecked ");
- }
- }
- });