How to use Android Tooglebutton and switch

Source: Internet
Author: User

These two are button switches. Monitor Checkedchangelistener

Toggle_layout.xml:

<?

XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation=" Vertical " android:id=" @+id/linealayout " > <!--define a ToggleButton button--><togglebutton Android:id= "@+id/toggle" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:textoff= "Horizontal arrangement" android:texton= "Portrait" android:checked= "true" /><switch android:id= "@ +id/switcher " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: textoff= "Horizontal arrangement" android:texton= "Portrait" android:thumb= "@drawable/login_button_selector" /></ Linearlayout>



Mainactivity.java:

Package Com.example.androidmy;import Android.os.bundle;import Android.provider.syncstatecontract.columns;import Android.app.activity;import Android.view.gravity;import Android.view.menu;import Android.widget.Button;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.compoundbutton;import Android.widget.gridlayout;import Android.widget.linearlayout;import Android.widget.switch;import Android.widget.togglebutton;public class Mainactivity extends Activity {private ToggleButton togglebutton;private Switch switch1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.toggle_layout); ToggleButton = (ToggleButton) Findviewbyid (r.id.toggle); switch1 = (Switch) Findviewbyid (R.id.switcher); final LinearLayout linearlayout = (linearlayout) Findviewbyid (r.id.linealayout); O Ncheckedchangelistener listener = new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (Compoundbutton Buttonview, BooleanisChecked) {if (isChecked) {//Set LinearLayout vertical Layout linearlayout.setorientation (1);} else{//Set LinearLayout horizontal layout linearlayout.setorientation (0);}}; Togglebutton.setoncheckedchangelistener (listener); Switch1.setoncheckedchangelistener (listener);}}

Effect:



How to use Android Tooglebutton and switch

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.