ToggleButton开关状态按钮控件

一、简介

1、

2、ToggleButton类结构

父类是CompoundButton,引包的时候注意下

二、ToggleButton开关状态按钮控件使用方法

1、新建ToggleButton控件及对象

private ToggleButton toggleButton1;

toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);

2、设置setOnCheckedChangeListener方法

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})

3、根据是否checked方法实现操作

if(isChecked){//开
  linearLayout1.setOrientation(LinearLayout.VERTICAL);
}
else{//关
  linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
}

三、代码实例

1、效果图:

开状态

关状态

2、代码:

fry.Activity01

 package fry;

 import com.example.ToggleButtonDemo1.R;

 import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton; public class Activity01 extends Activity{
private LinearLayout linearLayout1;
private ToggleButton toggleButton1; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01); linearLayout1=(LinearLayout) findViewById(R.id.linearLayout1);
toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);
/*
* ToggleButton开关状态按钮控件使用方法
* 1、新建ToggleButton控件及对象
* 2、设置setOnCheckedChangeListener方法
* 3、根据是否checked方法实现操作
*
*/
toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){//开
linearLayout1.setOrientation(LinearLayout.VERTICAL);
}
else{//关
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
}
}
}); }
}

/ToggleButtonDemo1/res/layout/activity01.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
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOn="横向排列"
android:textOff="纵向排列"
/>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout> </LinearLayout>

四、获得

1、

android:checked="true"

设置ToggleButton 状态

2、

android:textOn="横向排列"

设置ToggleButton打开文本

3、

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})

设置ToggleButton的setOnCheckedChangeListener方法

4、

if(isChecked)

判断ToggleButton状态开关

ToggleButton开关状态按钮控件的更多相关文章

  1. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

  2. WinRT自定义控件第一 - 转盘按钮控件

    之前的文章中,介绍了用WPF做一个转盘按钮控件,后来需要把这个控件移植到WinRT时,遇到了很大的问题,主要原因在于WPF和WinRT还是有很大不同的.这篇文章介绍了这个移植过程,由于2次实现的控件功 ...

  3. MFC编程入门之二十三(常用控件:按钮控件的编程实例)

    上一节讲了按钮控件Button.Radio Button和Check Box的基本用法,本节继续讲按钮控件的内容,通过一个实例让大家更清楚按钮控件在实际的软件开发中如何使用. 因为Button控件在前 ...

  4. MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...

  5. FineUI第五天---按钮控件

    按钮控件 <x:Button runat="server" ID="按下" Text="按下"></x:Button> ...

  6. VC按钮控件实现指示灯效果

    VC为按钮控件添加图片的方法有很多种: 直接调用SetBitmap:  CButton pButton->SetBitmap(hBitmap); 使用CButtonST控件: 使用CDC: 使用 ...

  7. Qt编写自定义控件11-设备防区按钮控件

    前言 在很多项目应用中,需要根据数据动态生成对象显示在地图上,比如地图标注,同时还需要可拖动对象到指定位置显示,能有多种状态指示,安防领域一般用来表示防区或者设备,可以直接显示防区号,有多种状态颜色指 ...

  8. Qt编写自定义控件9-导航按钮控件

    前言 导航按钮控件,主要用于各种漂亮精美的导航条,我们经常在web中看到导航条都非常精美,都是html+css+js实现的,还自带动画过度效果,Qt提供的qss其实也是无敌的,支持基本上所有的CSS2 ...

  9. 安卓开发_复选按钮控件(CheckBox)的简单使用

    复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于 ...

随机推荐

  1. Create a Group Policy Central Store

    一.How to create a Group Policy Central Store You have downloaded or created your own Group Policy Ad ...

  2. C#生成Windows服务

    1. 新建一个项目,或者从选择当前解决方案--右键-添加--新建项目 2. 选择Visual C#项目-->Windows 服务,填写要创建的服务名称(修改默认的WindowService1成为 ...

  3. Linux基础服务

    作业一:nginx服务1.二进制安装nginx包 [root@bogon ~]# systemctl disable firewalld #关闭Firewalld自启动 Removed symlink ...

  4. paper reading:gaze tracking

    https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Krafka_Eye_Tracking_for_CVPR_2016_ ...

  5. Django HttpResponse对象详解

    HttpResponse对象 Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数.那么视图函数在处理完相关的逻辑后,也需要返回一个响 ...

  6. java 使用LinkedList模拟一个堆栈或者队列数据结构

    近期在复习下java基础,看了下java基础,在看到集合时突然发现想起来曾经面试有一道笔试题:模拟一个堆栈或者队列数据结构,当时还没做出来,今天就写一下,首先得明确堆栈和队列的数据结构 堆栈:先进后出 ...

  7. Java中Collections和Collection的区别

    java.util.Collection Collection 层次结构 中的根接口.Collection 表示一组对象,这些对象也称为 collection 的元素.一些 collection 允许 ...

  8. An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

    与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider: TCP ...

  9. C++对象模型那点事儿(布局篇)

    1 前言 在C++中类的数据成员有两种:static和nonstatic.类的函数成员由三种:static,nonstatic和virtual. 上篇我们尽量说一些宏观上的东西,数据成员与函数成员在类 ...

  10. Redis六(管道)

    管道 为什么使用管道? Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处 ...