曹立东,电工电子技术基础,dgl031
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的改变等等。这样,我们的应用就会变得更加的灵活了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论