Android------togglebutton switch button

Source: Internet
Author: User


Mainactivity.java:

Import Android.app.activity;import android.os.bundle;import Android.view.menu;import Android.view.MenuItem;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import  Android.widget.linearlayout;import android.widget.togglebutton;/** * ToggleButton Switch Status Button control * * @author Caesar * */public Class Mainactivity extends Activity {private ToggleButton togglebuttonbutton;private linearlayout linearlayout;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Togglebuttonbutton = (ToggleButton) Findviewbyid (R.id.togglebutton); linearlayout = ( LinearLayout) Findviewbyid (r.id.mylayout); Togglebuttonbutton.setoncheckedchangelistener (new Myoncheckedchangelistener ());} public class Myoncheckedchangelistener implements oncheckedchangelistener{@Overridepublic void OnCheckedChanged ( Compoundbutton Buttonview,boolean isChecked) {//TODO auto-generated method stubif (isChecked) {lineaRlayout.setorientation (1)//= Set Vertical layout}else {linearlayout.setorientation (0);//Set Horizontal layout}}} 


Activity_main.xml:


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.togglebuttondemo.MainActivity "> <togglebutton android:id=" @+id/togglebutton "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:c Hecked= "true" android:textoff= "horizontal arrangement" android:texton= "vertical"/> <linearlayout android:id= "@+id /mylayout "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:orientati on= "vertical" > <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "button"/> <button android:id= "@+id/button2" android:layout_width= "Wrap_c            Ontent "android:layout_height=" wrap_content "android:text=" button "/> <button             Android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "button"/> </LinearLayout></LinearLayout>





Android------togglebutton switch button

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.