状态开关按钮togglebutton和开关switch

状态开关按钮togglebutton和开关switch是由button派生出来的,本质也是按钮,支持BUtton的各种属性,从功能上看,ToggleButton、Switch与CheckBox非常的相似,他们都可以提供两种状态,不过更常用的是切换程序中的某种状态。

   android:textOn="纵向排列"       -----状态打开时显示的文本
        android:textOff="横向排列"      -----状态关闭时显示的文本
        android:checked="true"        -----开关是否打开(true:打开)

--------案例:

(1)main.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" > <!-- 定义一个ToggleButton按钮 -->
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向排列"
android:textOn="纵向排列"
android:checked="true"
/>
<!-- 定义一个switch按钮 -->
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="纵向排列"
android:textOff="横向排列"
android:checked="true"
android:thumb="@drawable/toggle"
/> <LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="测试按钮一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮二"
android:textSize="15sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="测试按钮三"
/>
</LinearLayout> </LinearLayout>

(2)MainActivity.java

package com.yby.togglebutton;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton; /**
* 状态开关按钮togglebutton和开关switch
* @author yby
* description:状态开关按钮togglebutton和开关switch是由button派生出来的,
* togglebutton和switch通常用于切换程序中的某种状态
*/ public class MainActivity extends Activity{
private ToggleButton tgb;
private Switch switcher; /**
* 动态控制布局
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tgb = (ToggleButton) findViewById(R.id.toggle);
switcher = (Switch) findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
OnCheckedChangeListener listener = new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//设置垂直布局
test.setOrientation(1);
}else{
//设置水平布局
test.setOrientation(0);
}
}
};
//设置监听器
tgb.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
}

(3)效果图:纵向排列和横向排列

ToggleButton与Switch的更多相关文章

  1. 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...

  2. Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)

    ToggleButton 和Switch简介 ToggleButton 和Switch都是继承了Button,所以他们的属性设置和Button差不多. 分别支持的属性 ToggleButton 的属性 ...

  3. 一起学Android之ToggleButton和Switch

    本文以一个简单的小例子,简述在Android开发中ToggleButton(开关按钮)和Switch(开关)的简单使用,仅供学习分享使用. 概述 ToggleButton是一个有两种状态(checke ...

  4. UI组件之TextView及其子类(三)ToggleButton和Switch

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似. C ...

  5. Android零基础入门第21节:ToggleButton和Switch使用大全

    原文:Android零基础入门第21节:ToggleButton和Switch使用大全 上期学习了CheckBox和RadioButton,那么本期来学习Button的另外两个子控件ToggleBut ...

  6. ToggleButton 和 Switch

           界面: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  7. Android基础控件ToggleButton和Switch开关按钮

    1.简介 ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用! ToggleButton <!--checked 是否选择--> &l ...

  8. 状态开关按钮(ToggleButton)与开关(Switch)的功能与用法

    状态开关按钮(ToggleButton)与开关(Switch)也是由Button派生出来的,因此它们的本质也是按钮,Button支持的各种属性.方法也适用于ToggleButton和Switch.从功 ...

  9. Android学习笔记-开关按钮ToggleButton和开关Switch

    本节给大家介绍的Android基本UI控件是:开关按钮ToggleButton和开关Switch,这两个其实都是开关组件,只是后者需要在Android 4.0以后才能使用 所以AndroidManif ...

随机推荐

  1. Android crash特殊位置定位

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 通常情况下,在我们开发的过程中遇到的crash,可以到logcat中找原因:如果做定制App,对方用 ...

  2. INNO:检测程序是否已经安装,是则弹出卸载提示。

    INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...

  3. CI中的事物

    $this->db->trans_begin(), 在使用时,设置了  mysql AUTOCOMMIT = 0,表示禁止自动提交, 在未提交和回滚之前,不会提交sql查询. AUTOCO ...

  4. 【转】C++析构函数为什么要为虚函数

    注:本文内容来源于zhice163博文,感谢作者的整理. 1.为什么基类的析构函数是虚函数? 在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生. 下面转自网络:源地址  ...

  5. 数组右移(C++)

    数组A,右移k位,要求在线性时间内完成. #include <iostream> using namespace std; template <typename T> void ...

  6. 334. Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  8. 《C标准库》——之<ctype.h>

    在没读<ctype.h>的源码之前,我一直以为我们平时用的isalnum.isdigit.isalpha等这些函数,是靠判断写出来的. 比如: int isdigit(int c){ re ...

  9. codefoces round193a

    link: http://codeforces.com/contest/332/problem/A /* ID: zypz4571 LANG: C++ TASK: 193a.cpp */ #inclu ...

  10. 作业 for liao

    AD620 芯片 93 dB min Common-Mode Rejection Ratio (G = 10) 0.28 mV p-p Noise (0.1 Hz to 10 Hz) THEORY O ...