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 ...
随机推荐
- Python 入门学习笔记
安装和运行 官网下载安装包https://www.python.org/downloads/mac-osx/下载完直接安装即可 运行打开 terminal,输入命令 python,进入 python ...
- java字符串 64位编码
byte[] encodeBase64 = Base64.encodeBase64("到了是是是是".getBytes("UTF-8")); System.ou ...
- 查找算法总结Java实现
之前对查找算法做的一些简单总结与实现: 查找算法时间复杂度: 1.二分查找的实现(待补充) public class Test { //循环实现二分查找 public static int binar ...
- 【SPOJ-QTREE3】树链剖分
http://www.spoj.com/problems/QTREE3/ 时间限制:2s 代码长度限制:50000B 内存限制:1536MB [题目描述] 给出N个点的一棵树(N-1条边 ...
- bzoj 1089 SCOI2003严格n元树 递推
挺好想的,就是一直没调过,我也不知道哪儿的错,对拍也拍了,因为数据范围小,都快手动对拍了也不知道 哪儿错了.... 我们定义w[i]代表深度<=i的严格n元树的个数 那么最后w[d]-w[d-1 ...
- linux系统备份脚本
前言 之前写过<<linux系统简单备份的脚本>>, 最开始一直用着,后来觉得有必要改进下它,不管是从操作方式上还是脚本的工作方式上.之所以这么看重备份,是因为我经历过磁盘损坏 ...
- module加载过程初步分析[更新中]【转】
转自:http://blog.chinaunix.net/uid-1817735-id-2837068.html 分析这个过程可以有助于我们认识在加载模块时出现的问题大抵在哪里了. 直接从sys_in ...
- 我的一次安装oracle的过程
1.在装oracle之前,先安装.net3.5 2.然后正常安装oracle,一直next 3.装完oracle后,安装plsql dev工具,打开工具,发现没有connect as,是需要进行一些配 ...
- 【NppExec】直接在notepad++运行python的插件:NppExec
1.下载NppExec http://sourceforge.net/projects/npp-plugins/files/NppExec/ 2.安装 解压,全部复制到d:/program ...
- String.Format,DateTime日期时间格式化
DateTime dt = DateTime.Now;//2010年10月4日 17点05分 string str = ""; //st ...