ToggleButton控件,Switch控件
(一)
1.效果图
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context="com.example.app4.MainActivity"
android:orientation="vertical"> <ToggleButton
android:id="@+id/tb_button"
android:textOn="纵向布局"
android:textOff="横向布局"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
3.MainActivity.java
package com.example.app4; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private LinearLayout linearLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.tb_button);
linearLayout = (LinearLayout) findViewById(R.id.ll); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
}
}
});
}
}
(二)
1.效果图
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context="com.example.app4.MainActivity"
android:orientation="vertical"> <ToggleButton
android:id="@+id/tb_button"
android:textOn="纵向布局"
android:textOff="横向布局"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/sw"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
3.MainActivity.java
package com.example.app4; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private LinearLayout linearLayout;
private Switch aSwitch; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.tb_button);
linearLayout = (LinearLayout) findViewById(R.id.ll);
aSwitch = (Switch) findViewById(R.id.sw); MyListener myListener = new MyListener(); toggleButton.setOnCheckedChangeListener(myListener);
aSwitch.setOnCheckedChangeListener(myListener); /*toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
}
}
});*/
} class MyListener implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
aSwitch.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
aSwitch.setChecked(false);
}
}
}
}
ToggleButton控件,Switch控件的更多相关文章
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- 在tableviewcell里面嵌入switch控件以及如何获取switch控件数据
主要是通过cell.accessoryView来添加switch控件- (UITableViewCell *)tableView:(UITableView *)tableView cellForRow ...
- ToggleButton开关状态按钮控件
ToggleButton开关状态按钮控件 一.简介 1. 2.ToggleButton类结构 父类是CompoundButton,引包的时候注意下 二.ToggleButton开关状态按钮控件使用方法 ...
- Android Studio中Switch控件有关 textOn 和 textOff 用法
•属性 textOn:控件打开时显示的文字 textOff:控件关闭时显示的文字 showText:设置是否显示开关上的文字(API 21及以上) •用法 <?xml version=" ...
- 【原创】自己动手写控件----XSmartNote控件
一.前面的话 在上一篇博文自己动手写工具----XSmartNote [Beta 3.0]中,用到了若干个自定义控件,其中包含用于显示Note内容的简单的Label扩展控件,用于展示标签内容的labe ...
- WPF进阶技巧和实战03-控件(1-控件及内容控件)
所有控件都继承自System.Windows.Controls.Control类,这个类添加一些基本结构: 设置控件内容对齐方式 (HorizontalContentAlignment,Vertica ...
- winform用户控件、动态创建添加控件、timer控件、控件联动
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
- paip.gui控件tabs控件加载内容的原理以及easyui最佳实现
paip.gui控件tabs控件加载内容的原理以及easyui最佳实现 //////////////tabs控件的加载 同form窗体一样,俩个方式 两个方式:一个是url,简单的文本可以使用这个,不 ...
- zw版【转发·台湾nvp系列Delphi例程】Delphi 使用 HALCON库件COM控件数据格式转换
zw版[转发·台湾nvp系列Delphi例程]Delphi 使用 HALCON库件COM控件数据格式转换 Delphi 使用 HALCON库件COM控件数据格式转换,与IHObjectX接口有关 va ...
随机推荐
- 【BZOJ1419】 Red is good [期望DP]
Red is good Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 桌面上有R张红牌和B张 ...
- 求LCA最近公共祖先的在线倍增算法模板_C++
倍增求 LCA 是在线的,而且比 ST 好写多了,理解起来比 ST 和 Tarjan 都容易,于是就自行脑补吧,代码写得容易看懂 关键理解 f[i][j] 表示 i 号节点的第 2j 个父亲,也就是往 ...
- 【Mysql优化】key和index区别
mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的. 1).key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引 ...
- 关于hrtimer_forward小段代码的分析【转】
转自:http://blog.csdn.net/wowuyinglingluan/article/details/45720151 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?) ...
- chromium源代码下载(Win7x64+VS2013sp2, 39.0.2132.2)
chromium源代码下载(Win7x64+VS2013sp2, 39.0.2132.2) http://www.aichengxu.com/diannao/1000251.htm 前后折腾了四天,当 ...
- Quorum一致性协议
Quorum一致性协议 一个分布式数据库系统中通常是一系列密切关联的操作组成完整的系统. 分布式系统最基本的要保证一致性, 分区性通常是无法避免的, 在这种情况下尽力通过软件协议做到最大可用性. 根据 ...
- python基础复习-1-1文件类型、变量、运算符、表达式
文件类型: .py python源文件 由python解释器执行 .pyc python源码编译后生成的文件(字节代码) 编译方法: 源码文件中使用py_compile模块 import py_com ...
- JavaScript的for循环语句
语法格式 for(初始化值;循环的条件;每一次循环的递增值){ // 循环的条件结果为true,则执行循环体中的代码 } 示例(打印出1-10之间的整数): for(var a=1;a<=10; ...
- 【C语言】++(a++)的写法是错的
http://bbs.csdn.net/topics/390764053 a++得到的是一个右值,++操作需要的是一个左值. ------------------------------------- ...
- SQLAlchemy技术文档(中文版)-上
转自:http://www.cnblogs.com/iwangzc/p/4112078.html 1.版本检查 import sqlalchemy sqlalchemy.__version__ 2.连 ...