Switch and ToggleButton in Android

Source: Internet
Author: User

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

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.