ToggleButton and switch

Source: Internet
Author: User

Status Switch button ToggleButton and switching switch

Status switch button ToggleButton and switch switches are derived from the button, the essence is also buttons, support the various properties of the button, from a functional perspective, ToggleButton, switch and checkbox are very similar, They are available in two states, but it is more common to switch between certain States in the program.

android:texton= the text displayed when the "portrait"-----Status is open
android:textoff= the text that appears when the "horizontal arrangement"-----status is Closed
Android:checked= "true"-----switch is on (true: Open)

--------Case:

(1) Main.xml file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <!--Define a ToggleButton button -    <ToggleButtonAndroid:id= "@+id/toggle"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textoff= "Horizontal arrangement"Android:texton= "vertical arrangement"android:checked= "true"        />    <!--Define a switch button -    <SwitchAndroid:id= "@+id/switcher"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:texton= "vertical arrangement"Android:textoff= "Horizontal arrangement"android:checked= "true"Android:thumb= "@drawable/toggle"        />        <LinearLayoutAndroid:id= "@+id/test"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"        >        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "15SP"Android:text= "Test button One"        />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test button two"android:textsize= "15SP"        />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "15SP"Android:text= "Test button three"        />    </LinearLayout></LinearLayout>

(2) Mainactivity.java

 PackageCom.yby.togglebutton;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;Importandroid.widget.LinearLayout;ImportAndroid.widget.Switch;ImportAndroid.widget.ToggleButton;/*** Status Switch button ToggleButton and switching switch *@authoryby * Description: Status switch button ToggleButton and switch switches are derived from button, * ToggleButton and switch are typically used to toggle a state in a program*/ Public classMainactivityextendsactivity{PrivateToggleButton TGB; PrivateSwitch switcher; /*** Dynamic Control layout*/@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); TGB=(ToggleButton) Findviewbyid (R.id.toggle); Switcher=(Switch) Findviewbyid (R.id.switcher); FinalLinearLayout test =(LinearLayout) Findviewbyid (r.id.test);
Oncheckedchangelistener Listener=NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {//Set Vertical LayoutTest.setorientation (1); }Else{ //Set Horizontal LayoutTest.setorientation (0); } } }; //Setting up ListenersTgb.setoncheckedchangelistener (listener); Switcher.setoncheckedchangelistener (listener); }}

(3): Vertical and horizontal arrangement

ToggleButton 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.