码迷,mamicode.com
首页 > 移动开发 > 详细

Android---19---ToggleButton开关按钮

时间:2015-04-17 20:33:20      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:android开发   togglebutton   


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 开关状态按钮控件
 * 
 * @author Caesar
 * 
 */
public class MainActivity extends Activity {
	private ToggleButton toggleButtonbutton;
	private LinearLayout linearLayout;

	@Override
	protected 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{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked){
				linearLayout.setOrientation(1);//表示设置垂直布局
			}else {
				linearLayout.setOrientation(0);//设置水平布局
			}
		}
		
	}
}


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:orientation="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:checked="true"
        android:textOff="横向排列"
        android:textOn="纵向排列" />

    <LinearLayout
        android:id="@+id/mylayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="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_content"
            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---19---ToggleButton开关按钮

标签:android开发   togglebutton   

原文地址:http://blog.csdn.net/u013476556/article/details/45101277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!