根据使用场景的不同,可能需要使用系统默认的CheckBox样式,但可能需要自定义CheckBox样式。 今天主要学习如何定制CheckBox样式。 当CheckBox的状态发生变化时,OnCheckedChangeListener可能需要执行其他任务。 所以今天的内容如下。
1、系统默认样式
2、定制checkBox风格
3、使用OnCheckedChangeListener侦听程序侦听CheckBox的选择状态。
我们先来看看效果图:
图1和图2
图1是默认和自定义样式,当checkBox的checked状态更改时,将弹出Toast提示,如图2所示。
我不能说任何系统的默认样式。 这里主要介绍如何定制自己的风格
首先准备两张照片:
check_box_normal.jpg
check_box_checked.jpg
1、定义selector,然后单击check_box_style.xml :
2、定义CheckBox样式:
@drawable/check_box_style
@color/text_view_stroke
3、在checkBox中使用此样式:
Android : id=' @ id/show _ checkbox _ style _ customer '
安卓: layout _ width=' wrap _ content '
Android : layout _ height=' wrap _ content '
Android : text=' @ string/customer _ checkbox _ style '
style=' @ style/check _ box _ theme ' /
4、拦截CheckBox状态变化
mcheckboxcustomer.setoncheckedchangelistener (this;
选择状态发生变化,doSometing
publicvoidoncheckedchanged (compoundbuttonbuttonview,boolean isChecked ) {
toast.maketext(this,' check box state changed!toast.Length_long(.show );
}
详细代码如下所示。
1、布局文件构件_ checkbox _ layout.XML
Android : layout _ width=' match _ parent '
Android : layout _ height=' match _ parent '
Android : gravity=' center _ horizontal '
Android : orientation=' vertical '
Android : id=' @ id/show _ checkbox _ style '
安卓: layout _ width=' wrap _ content '
Android : layout _ height=' wrap _ content '
Android : text=' @ string/default _ checkbox _ style ' /
Android : id=' @ id/show _ checkbox _ style _ customer '
安卓: layout _ width=' wrap _ content '
Android : layout _ height=' wrap _ content '
Android : text=' @ string/customer _ checkbox _ style '
style=' @ style/check _ box _ theme ' /
2、activity —— widgetcheckboxactivity.Java
package com.xy.ZT.selfdefinewieget;
导入安卓. app.activity;
导入安卓. OS.bundle;
import Android .构件. checkbox;
import Android .构件.compound button;
import Android .构件. toast;
import Android .构件.compound button.oncheckedchangelistener;
publicclasswidgetcheckboxactivityextendsactivityimplementsoncheckedchangelistener {
CheckBox mCheckBoxDefault;
CheckBox mCheckBoxCustomer;
@Override
公共void oncreate (绑定验证例程状态)。
super.oncreate (savedinstancestate;
setcontentview (r.layout.widget _ checkbox _ layout;
init (;
}
私有语音init (
mcheckboxdefault=(checkbox ) findviewbyid (r.id.show _ checkbox _ style );
mcheckboxdefault.setoncheckedchangelistener;
mCheckBoxCustomer=(CheckBox ) findviewbyid (r.id.show _ checkbox _ style _ customer );
mcheckboxcustomer.setoncheckedchangelistener (this;
}
publicvoidoncheckedchanged (compoundbuttonbuttonview,boolean isChecked ) {
toast.maketext(this,' check box state changed!toast.Length_long(.show );
}
}
3、在ViewData.java和WidgetsAdapter中分别添加以下内容:
ViewData.java
publicstaticfinalintcheck _ box _ id=degi tal _ clock _ id1;
publicstaticfinalstringcheck _ box _ name=' checkbox ';
privatestaticfinalviewdatamcheckbox=newviewdata (check _ box _ name,
CHECK_BOX_ID;
view_datas.add(mcheckbox;
构件适配器的handleItemClicked方法:
case ViewData.CHECK_BOX_ID:
intent.setclass(mcontext,WidgetCheckBoxActivity.class;
mcontext.startactivity(intent );
布雷克;
CheckBox控件学到了这里。 在下一篇文章中,您将学习射频按钮射频组。