Android ToggleButton 详解及实例代码
作者:lqh 时间:2022-09-11 03:58:02
Android ToggleButton 详解
在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来说一下,这个组件的两种使用方式。
第一种是简单的使用,利用Toast的方式弹出提示语句
需要注意的是要想自定义ToggleButton的显示的内容,就需要设置其TextOn和TextOff的内容。
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/toggleButton2"
android:layout_alignBottom="@+id/toggleButton2"
android:textOn="开"
android:textOff="关"
android:layout_alignRight="@+id/imageview"
android:text="Simple test" />
然后是主要的显示代码:
case R.id.toggleButton1:
if(SimpleTest.isChecked()){
Toast.makeText(getApplication(), "你打开了开按钮", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplication(), "你打开了关按钮", Toast.LENGTH_SHORT).show();
}
break;
//应该注意的是,先声明ToggleButton并初始化,然后注册侦听方法
接下来是一个较为复杂一点的使用案例,那就是配合ImageView来实现不同的图片显示状态
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageview"
android:layout_alignParentTop="true"
android:layout_marginTop="46dp"
android:textOn="美女"
android:textOff="图标"
android:text="With Image" />
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/note"
android:layout_below="@id/toggleButton2"
/>
然后是活动代码
case R.id.toggleButton2:
if(WithImage.isChecked()){
imageview.setImageResource(R.drawable.note);
}else{
imageview.setImageResource(R.drawable.ic_launcher);
}
break;
需要注意的是,我们同样需要先进行声明,才能对其使用,否则会报空指针的错误。
下面是程序运行之后的结果
总结与设想:
在使用过程中使用到的ToggleButton 一般来说不会这么的简单,但是主要的思想和框架还是基于这里的。我们可以在相关的侦听方法中添加比如静音的处理,或者status的改变等等。这样,我们的应用就会变得更加的灵活了。
来源:http://blog.csdn.net/marksinoberg/article/details/50231641
标签:Android,ToggleButton
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中的委托和事件
2023-11-27 12:33:34
![](https://img.aspxhome.com/file/2023/9/113699_0s.jpg)
Android添加用户组及自定义App权限的方法
2022-09-15 20:08:53
![](https://img.aspxhome.com/file/2023/8/117118_0s.jpg)
SpringMVC+MyBatis 事务管理(实例)
2022-07-30 11:04:40
![](https://img.aspxhome.com/file/2023/9/65579_0s.png)
Android绘图常用方法汇总
2023-11-19 20:06:43
SpringBoot集成gRPC微服务工程搭建实践的方法
2022-03-11 22:10:39
Kotlin 协程异步热数据流的设计与使用讲解
2022-09-21 18:30:32
![](https://img.aspxhome.com/file/2023/4/138644_0s.jpg)
详解C# 虚方法virtual
2022-11-05 09:41:17
Android DownloadProvider 源码详解
2023-02-11 03:26:17
如何通过zuul添加或修改请求参数
2022-08-13 05:05:20
Intellij Idea 多模块Maven工程中模块之间无法相互引用问题
2023-11-11 07:24:51
![](https://img.aspxhome.com/file/2023/0/59230_0s.jpg)
Spring Security 构建rest服务实现rememberme 记住我功能
2023-03-29 14:42:03
![](https://img.aspxhome.com/file/2023/1/81961_0s.jpg)
将cantk runtime嵌入到现有的APP中的方法
2021-12-04 06:06:09
Android 优雅的实现通用格式化编辑
2023-02-08 05:24:02
![](https://img.aspxhome.com/file/2023/8/130458_0s.gif)
Spring MVC项目中log4J和AOP使用详解
2022-11-16 08:36:29
![](https://img.aspxhome.com/file/2023/0/76540_0s.png)
利用Jetpack Compose实现绘制五角星效果
2023-04-10 06:20:48
![](https://img.aspxhome.com/file/2023/7/137847_0s.png)
c#编写webservice服务引用实例分享
2023-05-11 19:28:41
SpringBoot整合Shiro两种方式(总结)
2021-09-08 21:28:24
![](https://img.aspxhome.com/file/2023/6/65146_0s.png)
Unity3D使用UGUI开发原生虚拟摇杆
2021-10-04 17:28:26
![](https://img.aspxhome.com/file/2023/6/91236_0s.gif)
java实现输入输出流代码分享
2023-11-18 01:03:45
关于JAVA11中图片与BASE64相互转换的实现
2022-12-06 06:44:11
![](https://img.aspxhome.com/file/2023/6/73086_0s.png)