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