ToggleButton与Switch

时间:2021-11-25 17:59:35

状态开关按钮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与SwitchToggleButton与Switch

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. ajax 请求 对json传的处理 Jquery 使用Ajax获取后台返回的Json数据后&comma;页面处理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 将默认的Netbeans中文版设置为英文界面

    问题:从官网下载的Netbeans不论是中文版还是英文版默认的操作界面都是中文,并且字体十分恶心. 原因:Netbeans 根据本地的操作系统自动设置界面语言,并且没有提供更改的功能. 目标效果:把N ...

  3. CentOS 6&period;4 x64 postfix &plus; dovecot &plus; 虚拟用户认证

    第一, 首先必须安装 apacache  mysql  php CentOS 直接使用 yum 安装 yum -y install httpd httpd-devel mysql php-mysql  ...

  4. Android平台targetSdkVersion设置及动态权限

    --关于Android动态权限和targetSdkVersion Android系统自6.0开始,提供动态权限机制,对于敏感权限(存储,定位,录音,拍照,录像等),需要在APP运行过程中动态向用户申请 ...

  5. netcorec程序部署配置

    IIS方式: 1:iis配置netcore发布的文件 2:iis设置运行库无托管模式 3:安装DotNetCore.1.0.4_1.1.1-WindowsHosting.exe 4:安装dotnet- ...

  6. Maven 的安装与配置

    最近公司需要新起一个项目,想使用maven+springmvc+spring+mybatis+mysql实现,以前我们的项目都是传统的老项目,没用过maven,Eclipse版本是GALILEO的,有 ...

  7. Python 字符编码简记

    名称 说明 ASCII 只能存英文和拉丁字符,一个字符占一个字节,8位. ASCII 码是不支持中文的,支持中文的第一张表是 GB2312 GB2312 支持中文,收录了 7445个字符 GBK1.0 ...

  8. FieldGroup绑定的日期类型存储格式的问题

    问题 日期存储的时候,当前数据库中存储格式为 "2017-9-5 0:00:00", 而我实现了以后,看到数据库的存储格式为 "Mon Sep 04 00:00:00 C ...

  9. Style、ControlTemplate 和 DataTemplate 触发器

    本文摘要:    1:属性触发器:    2:数据触发器:    3:事件触发器: Style.ControlTemplate 和 DataTemplate 都有触发器集合.    属性触发器只检查W ...

  10. 如何从 GitHub 上下载单个文件夹?

    比如别人把一些资料传到 GitHub 上做分类归档,我怎么才能下载单一分类文件夹? 点击 Raw ,如果是不能预览的文件就会自动下载,如果是文件或者代码什么的,会在浏览器中显示,但可以复制浏览器中链接 ...