The Status switch button ToggleButton and switch switches are all derived from the button, and all properties and methods of the button apply, typically for state switching.
1) activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<!--define a status switch button that is arranged horizontally when the switch is off and is arranged vertically when the switch is turned on
<togglebutton android:id= "@+id/toggle"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:textoff= "Horizontal arrangement"
android:texton= "vertical Arrangement"
Android:checked= "true"/>
<!--defines a switch that is arranged horizontally when the switch is off and is arranged vertically when the switch is turned on
<switch
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/switcher"
android:textoff= "Horizontal arrangement"
android:texton= "vertical Arrangement"
android:thumb= "@drawable/check"
Android:checked= "true"/>
<!--defines a linear layout with three buttons, the default arrangement is portrait-to-
<linearlayout
Android:layout_width= "Match_parent"
Android:id= "@+id/test"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "button 1"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "button 2"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "button 3"/>
</LinearLayout>
</LinearLayout>
2) Mainactivity.java
Import android.app.Activity;
Import Android.os.Bundle;
Import android.widget.*;
public class Mainactivity extends Activity {
ToggleButton Toggle;
Switch switcher;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Toggle= (ToggleButton) Findviewbyid (R.id.toggle);
Switcher= (Switch) Findviewbyid (R.id.switcher);
Final LinearLayout test= (linearlayout) Findviewbyid (r.id.test);
/**
Compoundbutton is a button with a checked/unchecked state that automatically changes state when the button is pressed. ToggleButton and switch are inherited from it. The ischecked is used to indicate whether the button is selected.
*/
Compoundbutton.oncheckedchangelistener listener=new Compoundbutton.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton button,boolean isChecked) {
if (isChecked) {
Test.setorientation (1);//Linear layout is vertical.
Toggle.setchecked (TRUE);
Switcher.setchecked (TRUE);
}
else{
Test.setorientation (0);//Linear layout is horizontal.
Toggle.setchecked (FALSE);
Switcher.setchecked (FALSE);
}
}
};
Toggle.setoncheckedchangelistener (listener);
Switcher.setoncheckedchangelistener (listener);
}
}
Operation Result:
1) When the switch is turned on:
2) When the switch is off:
Switch and ToggleButton in Android