状态开关按钮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. Nearest number - 2_暴力&&bfs

    Description Input is the matrix A of N by N non-negative integers. A distance between two elements A ...

  2. EnterpriseLibrary4 自己封装程序集实现log打印

      注意:1)要引用响应的程序集,必须是41的          2)配置文件 using Microsoft.Practices.EnterpriseLibrary.Common.Configura ...

  3. Disable Portrait in app

    I had this problem as well as I wanted to constrain my game to only landscape mode. I put this in my ...

  4. Sublime text2如何设置快捷键让编写的HTML文件在浏览器预览?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:浪人链接:http://www.zhihu.com/question/27219231/answer/43608776来源:知 ...

  5. js 下载文件 window.location.href

    window.location.href ="../../pages2/assessmentplan/exportPointAsessment.do?planId="+planId ...

  6. Java 存储过程调用

    //配置文件 private static ClientServiceConfigUtil configUtil = new ClientServiceConfigUtil("/Databa ...

  7. php 滑动验证码

    自己研究: jQuery拖拽滑动验证码插件 slideunlock.js 原理:(别人说) 响应时间,拖拽速度,时间,位置,轨迹,重试次数等.这些因素能够构成一个采样结果或者辨识特性. 只获取到滑动时 ...

  8. 带同时滚动小色条的banner轮播图jq

    <div class="baoliao tongcheng"> <p class="headline1">同城<font>活 ...

  9. 【转】beancopy的替代方案

    链接:http://jingyan.baidu.com/article/215817f7d55b871edb14235b.html 最近在项目中接触到了BeanUtils.copyProperties ...

  10. 安装arbotix simulator仿真环境--9

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/  周学伟 安装之前:首先确保已经正常制作了ros工作空间并且安装了rbx1功能包: cd ~/catki ...