`
小小米081189
  • 浏览: 20116 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

疯狂android讲义学习总结---toggle与switch

 
阅读更多
ToggleButton 与Switch二者都是Button的子类,与RadioButton和CheckBox不同的是,ToggleButton 与Switch二者通常用作切换程序中的某种状态
ToggleButton 与Switch二者常用的xml属性是  android:checked;android:textOn;android:textOff;
而Switch常用的属性还有,android:thumb;
由于Switch是android4.0以后才有的版本,所以用该按钮的时候 一定要将项目的anroid版本调高一些,或者将AVD的API调高一些。才不会出现问题,本文利用这两个按钮来控制页面布局,这里用到了<LinearLayout/>
====================================================================================
xml代码


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义一个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/check"
android:checked="true"/>
<!-- 定义一个可以动态改变方向的线性布局 -->
<LinearLayout android:id="@+id/test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"==将这三个按钮放在了一个LinearLayout中
android:text="测试按钮二"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮三"
/>
</LinearLayout>
</LinearLayout>
==================================================================================
java代码
package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author  Yeeku.H.Lee kongyeeku@163.com
* @version  1.0
*/
public class ToggleButtonTest extends Activity
{
ToggleButton toggle;
Switch switcher;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton)findViewById(R.id.toggle);
switcher = (Switch)findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);
Log.i("j","jhkjh");
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);
}

}
});
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);
Log.i("j","jhkjh");
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);
}

}
});
}
}
  • 大小: 10.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics