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

android的toogleButton和switch的用法

时间:2014-10-20 11:46:59      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   io   os   ar   java   2014   on   

这两个是按钮开关,监听CheckedChangeListener

toggle_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/lineaLayout"
    >
    <!-- 定义一个ToggleButton按钮 -->
	<ToggleButton android:id="@+id/toggle"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textOff="横向排列"
	    android:textOn="纵向排列"
	    android:checked="true"
	    />
	
	<Switch android:id="@+id/switcher"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textOff="横向排列"
	    android:textOn="纵向排列"
	    android:thumb="@drawable/login_button_selector"
	    />
</LinearLayout>


MainActivity.java:

package com.example.androidmy;

import android.os.Bundle;
import android.provider.SyncStateContract.Columns;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	private ToggleButton toggleButton;
	private Switch switch1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			setContentView(R.layout.toggle_layout);
			toggleButton = (ToggleButton)findViewById(R.id.toggle);
			switch1 = (Switch)findViewById(R.id.switcher);
			final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.lineaLayout);
			OnCheckedChangeListener listener = new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
						//设置LinearLayout垂直布局
						linearLayout.setOrientation(1);
					}else{
						//设置LinearLayout水平布局
						linearLayout.setOrientation(0);
					}
					
				}
			};
			toggleButton.setOnCheckedChangeListener(listener);
			switch1.setOnCheckedChangeListener(listener);
		}
	
	
	
	
	
	}

	

效果:

bubuko.com,布布扣

bubuko.com,布布扣

android的toogleButton和switch的用法

标签:android   blog   http   io   os   ar   java   2014   on   

原文地址:http://blog.csdn.net/howlaa/article/details/40297323

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