[Base Control]---State toggle control Compoundbutton and its subclasses checkbox, RadioButton, ToggleButton, switch event listener and scenario usage

Source: Internet
Author: User
Tags event listener

First, event monitoring

For the ordinary button, the event monitoring of the Google official gives a common three kinds of listening methods: 1, for each button set event listener Button.setonclicklistener (View.onclicklistener listener); This method is much more, messy, and less concise when button buttons are large.

2, implement Interface View.onclicklistener in activity, then rewrite the void OnClick (View v) method, and use switch (V.getid ()) to distinguish different buttons in the method. This method is more concise, but requires the implementation of the View.onclicklistener interface. 3, in the XML layout in want to be listening to the

Add Property on button: android:onclick= "DoClick" property. Adding a listening method to the activity public void DoClick (view view) {}, this method writes simple, straightforward, and does not require additional interfaces to be implemented. This is the recommended method of use. It is also a common use in Google's official documentation.

For the status switch control Compoundbutton, it not only listens for event triggering, but also listens for state switching. Therefore, it is necessary to do two monitoring in Compoundbutton: event triggering, state switching. The mode of listening is similar to the normal button three listening mode. It's just one more of a listening state.

Only. Say more is a nonsense, or directly on the code.

Scenario One: The event listener for multiple Compoundbutton on the UI interface is handled uniformly.

<ToggleButtonAndroid:id= "@+id/togglebutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:textoff= "Off"Android:texton= "Open" />    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" >        <CheckBoxAndroid:id= "@+id/checkbox_meat"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Meat" />        <CheckBoxAndroid:id= "@+id/checkbox_cheese"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Milk" />    </LinearLayout>    <RadiogroupAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >        <RadioButtonAndroid:id= "@+id/radiobutton_add"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Increase" />        <RadioButtonAndroid:id= "@+id/radiobutton_delete"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Delete" />        <RadioButtonAndroid:id= "@+id/radiobutton_update"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Change" />        <RadioButtonAndroid:id= "@+id/radiobutton_seach"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "DoClick"Android:text= "Check" />    </Radiogroup>

With the layout, the following Java code monitors all of its Compoundbutton controls

/*** The purpose of the upward transformation is to get the current state of the child controls. * @paramView*/     Public voidDoClick (view view) {//1, was selected: Toogle in Ischecked==on        BooleanIschecked= ((Compoundbutton) view). isChecked ();//transition up: Get current status//2. Be clicked        Switch(View.getid ()) { CaseR.id.togglebutton:if(isChecked) {log.i ("MyInfo", "open"); }Else{log.i ("MyInfo", "Off"); }             Break;  Caser.id.checkbox_meat:if(isChecked) {log.i ("MyInfo", "Meat is selected"); }Else{log.i ("MyInfo", "Meat is canceled."); }             Break;  CaseR.id.checkbox_cheese: Break;  CaseR.id.radiobutton_add://remember: RadioButton switch without a state, only the switch of the button. So just determine if the selected state if (isChecked)            if(isChecked) Break;  CaseR.id.radiobutton_delete:if(isChecked) Break;  Caser.id.radiobutton_update:if(isChecked) Break;  CaseR.id.radiobutton_seach:if(isChecked) Break; default:             Break; }    }

The overall two steps were performed in the DoClick () method: 1 was selected---->2 was clicked. Usually the sequence of these two steps should be clicked-----> be selected. However, this requires that the code appear duplicated in each sub-control branch that needs to be added whether it is selected.

So here we use the inverse to be clicked-----> be selected. Use an upward transformation in the selected step in order to get the status of all Compoundbutton subclasses. If the direct strong to a specific subclass, it is not universal, not fit to judge all Compoundbutton

The selected state of the child class.

When the state switch control Compoundbuuton and the normal button are present in the UI interface, it is recommended to listen for different methods for the two controls, for example: android:onclick= "Compoundbuttonclick" With android:onclick= "ButtonClick"

Second, Compoundbutton expansion

---not to be continued

[Base Control]---State toggle control Compoundbutton and its subclasses checkbox, RadioButton, ToggleButton, switch event listener and scenario usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.