android开关控件Switch和ToggleButton
序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下。
1.Switch
<Switch
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开启"
android:layout_marginLeft="20dp"
android:textOff="关闭"
android:showText="true"
android:thumb="@drawable/shape_thum"
android:track="@drawable/select_bg_switch"
/>

这里layout_width:这能设置整个布局的宽度,不能设置具体的Switch的大小,需要使用switchMinWidth属性来设置。
thumb:文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。
track:设置开关的背景图片,类似于button的background。
textoff、texton:设置开关时的文字显示。
2.ToggleButton
<ToggleButton
android:layout_width="80dp"
android:layout_height="20dp"
android:id="@+id/toggle"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_track_on"
android:textOff="off"
android:textOn="on"
android:layout_marginLeft="20dp" />
3.效果图

1.布局
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_marginTop="30dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开关"
/>
<ToggleButton
android:layout_width="80dp"
android:layout_height="20dp"
android:id="@+id/toggle"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_track_on"
android:textOff="off"
android:textOn="on"
android:layout_marginLeft="20dp" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启状态"
/> <Switch
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开启"
android:layout_marginLeft="20dp"
android:textOff="关闭"
android:showText="true"
android:thumb="@drawable/shape_thum"
android:track="@drawable/select_bg_switch"
/> </LinearLayout> </RelativeLayout>
shape_thum.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#0f0"/>
<size android:height="30dp" android:width="30dp"/>
</shape>
shape_track_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="30dp"/>
<solid android:color="@android:color/darker_gray"/>
</shape>
shape_track_on,xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="30dp"/>
<solid android:color="@android:color/holo_red_light"/>
</shape>
select_bg_switch.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/shape_track_on"/>
<item android:state_checked="false" android:drawable="@drawable/shape_track_off"/> </selector>
Mainactivity
public class MainActivity extends AppCompatActivity {
private Switch aSwitch;
private ToggleButton toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aSwitch=findViewById(R.id.bt);
toggle=findViewById(R.id.toggle);
//状态改变监听
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();
}
}
});
//状态改变监听
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();
}
}
});
}
}
android开关控件Switch和ToggleButton的更多相关文章
- android自己定义开关控件
近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图: waterma ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android之控件使用
Android系统为我们提供了大量的控件,例如:开关控件.单选按钮.多选按钮.单选菜单等等,那么这些控件如何使用呢?本篇我将带领大家一道学习一下如何使用这些控件.所谓无图无真相,先让大家看一下效果图: ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- Android基本控件之Menus
在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...
- UISwitch 开关控件
UISwitch iOS中的开关控件,只有两种状态,打开或关闭. aSwitch.tintColor = [UIColor redColor]; //关闭状态下的渲染颜色 aSwitch.onTint ...
- Android给控件添加触摸回调
Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...
- weui-switch开关控件,表单提交后如何取值
最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...
- [Xcode 实际操作]四、常用控件-(6)UISwitch开关控件的使用
目录:[Swift]Xcode实际操作 本文将演示开关控件的基本用法. 开关控件有两个互斥的选项,它是用来打开或关闭选项的控件. 在项目导航区,打开视图控制器的代码文件[ViewController. ...
随机推荐
- B/S与C/S的区别
参考:http://www.cnblogs.com/groler/articles/2116905.html 一.概念 C/S结构:即Client/Server(客户机/服务器)结构,是大家熟知的软件 ...
- 关联数组的错误,mysql insert varchar 原生的错误
在写代码的时候,没注意犯了2个低级错误: 关联数组的错误 $array = ['id' => '03657', 'kf_phone ' => 18796442]; 然后你再读取的时候就需要 ...
- ES之四、Elasticsearch集群和索引常用命令
REST API用途 elasticsearch支持多种通讯,其中包括http请求响应服务,因此通过curl命令,可以发送http请求,并得到json返回内容. ES提供了很多全面的API,常用的RE ...
- AngularJS:Scope(作用域)
ylbtech-AngularJS:Scope(作用域) 1.返回顶部 1. AngularJS Scope(作用域) Scope(作用域) 是应用在 HTML (视图) 和 JavaScript ( ...
- Oracle 常见hint
Hints 应该慎用,收集相关表的统计信息,根据执行计划,来改变查询方式 只能在SELECT, UPDATE, INSERT, MERGE, or DELETE 关键字后面,只有insert可以用2个 ...
- 委托BegionInvoke和窗体BegionInvoke
委托BegionInvoke是指通过委托方法执行多线程任务,例如: //定义委托成员变量 delegate void dg_DeleAirport(); //指定委托函数 dg_DeleAirpor ...
- list map set 集合的区别
Java中的集合包括三大类,它们是Set.List和Map,它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现 类.Set的实现类主要有HashSet和TreeSet ...
- 解决maven构建工程错误:Failure to transfer org.apache.maven.plugins:maven-jar-plugin:pom:2.4 from错误
问题描述: mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=ma ...
- HTML5 使用sessionStorage实现页面返回刷新
需求:在某个列表页面跳转到增加新项目页面后需要返回到前一个页面 并且数据最新数据.刚开始是做法是 history.back();方法 返回后页面不会自动刷新的.在新的页面重新访问之前页面的链接可以访问 ...
- php SqlServer 中文汉字乱码
php SqlServer 中文汉字乱码,用iconv函数转换 查询显示的时候,从GB转换为UTF8 <?php echo iconv('GB2312','UTF-8',$row['Name'] ...