状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
2016-03-10 09:46
507 查看
状态开关按钮(ToggleButton)和开关(Switch)也是由Button派生出来的,因此他们的本质也是那妞,Button支持的各种属性、方法也适用于ToggleButton和Switch。
从功能上看,ToggleButton、Switch和CheckBox复选框非常相似,他们都可以提供两种状态。不过ToggleButton、Switch与CheckBox的区别主要体现在功能上,ToggleButton、Switch通常用于切换程序中的某种状态。
实例:动态控制布局。
界面布局文件如下。
MainActivity.java代码如下。
运行结果如下。
从功能上看,ToggleButton、Switch和CheckBox复选框非常相似,他们都可以提供两种状态。不过ToggleButton、Switch与CheckBox的区别主要体现在功能上,ToggleButton、Switch通常用于切换程序中的某种状态。
实例:动态控制布局。
界面布局文件如下。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.l2112.togglebuttontest.MainActivity" tools:showIn="@layout/activity_main" android:orientation="vertical"> <!--定义一个ToggleButton按钮--> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/toggle" android:textOff="横向排列" android:textOn="纵向排列" android:checked="true"/> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/switcher" android:textOff="横向排列" android:textOn="纵向排列" android:thumb="@drawable/check" android:checked="true"/> <!--定义一个可以动态改变方向的线性布局--> <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:text="测试按钮一"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮二"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮三"/> </LinearLayout> </LinearLayout>
MainActivity.java代码如下。
package com.example.l2112.togglebuttontest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.ToggleButton; import com.example.l2112.togglebuttontest.R; public class MainActivity extends Activity { ToggleButton toggle; Switch switcher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); toggle = (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 button , boolean isChecked) { if(isChecked) { // 设置LinearLayout垂直布局 test.setOrientation(LinearLayout.VERTICAL); toggle.setChecked(true); switcher.setChecked(true); } else { // 设置LinearLayout水平布局 test.setOrientation(LinearLayout.HORIZONTAL); toggle.setChecked(false); switcher.setChecked(false); } } }; toggle.setOnCheckedChangeListener(listener); switcher.setOnCheckedChangeListener(listener); } }
运行结果如下。
相关文章推荐
- java面向对象之封装
- 重整晋商雄风,再现汇通天下 -------《晋商》阅读笔记第1-4章
- springmvc学习笔记(8)-springmvc整合mybatis之service
- 第一次3-1
- docker 安装跟基本操作
- 第一次实验1-2
- 《企业网络》评选的十大WLAN设备商
- c++第一次上机实验--2
- 最常用的PHP正则表达式收集整理
- 20160310
- HCMPRD数据库恢复HCMPRD RAC数据库
- MYSQL 导入出错问题
- .NET Memory usage – A restaurant analogy
- 最简单的基于Flash的流媒体示例:网页播放器(HTTP,RTMP,HLS)
- 2016.3.8__HTML 基础_第二天
- C++第一次实验--(1)
- c++上机报告
- C++第一次课堂实验-2
- MyCat源码分析系列之——结果合并
- Unity doesn't work on VNC server under Ubuntu 14.04 LTS ubuntu 14.04unity 桌面 和vnc