5.9.5UICheckBox

CheckBox是原生控件没有提供的
CheckBox * CheckBox ::create( const std :: string & backGround,
                          
const std :: string & backGroundSeleted,
                          
const std :: string & cross,
                          
const std :: string & backGroundDisabled,
                          
const std :: string & frontCrossDisabled,
                           TextureResType texType)
backGround          普通状态下图片
backGroundSeleted    普通状态下被点击时图片
cross                 激活状态下图片
backGroundDisabled   正常状态下不可用时图片
frontCrossDisabled   激活状态下被点击时图片

texType             纹理资源类型
void CheckBox ::addEventListenerCheckBox( Ref *target, SEL_SelectedStateEvent selector)
{
   
_checkBoxEventListener = target;
   
_checkBoxEventSelector = selector;
}

void CheckBox ::addEventListener( const ccCheckBoxCallback & callback)
{
   
_checkBoxEventCallback = callback;
}
代码:

#include "ui/CocosGUI.h" //******
using namespace cocos2d :: ui ;
    void selectedEvent( Ref * pSender, CheckBox :: EventType type);
private :
    Size size;
    CheckBox* checkBox1;
    CheckBox* checkBox2;

#include "ui/CocosGUI.h" //******
using namespace cocos2d :: ui ;
size = Director :: getInstance ()-> getWinSize ();
    checkBox1 = CheckBox :: create ();
   
checkBox1 -> setTouchEnabled ( true );
   
///cocos2d-x-3.3/tests/cpp-tests/Resources/ccs-res/cocosui/check_box_active_press.png
   
checkBox1 -> loadTextures ( "check_box_normal.png" ,
                           
"check_box_normal_press.png" ,
                           
"check_box_active.png" ,
                           
"check_box_normal_disable.png" ,
                           
"check_box_active_disable.png" );
   
checkBox1 -> setPosition ( Vec2 ( size . width * 0.3 , size . height * 0.5 ));
   
// 添加监听事件
   
checkBox1 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
   
addChild ( checkBox1 );
   
// 设置初始状态为选择状态
   
checkBox1 -> setSelected ( true );
   
checkBox1 -> setTag ( 1 );
   
   
// 添加文本
   
auto label1 = Text :: create ();
    label1->
setFontSize ( 20 );
    label1->
setPosition ( Vec2 ( size . width * 0.38 , size . height * 0.5 ));
    label1->
setString ( "Male" );
   
addChild (label1);
   

    // 添加 Female 单选框
   
checkBox2 = CheckBox :: create ();
   
checkBox2 -> setTouchEnabled ( true );
   
checkBox2 -> loadTextures ( "check_box_normal.png" ,
                           
"check_box_normal_press.png" ,
                           
"check_box_active.png" ,
                           
"check_box_normal_disable.png" ,
                           
"check_box_active_disable.png" );
   
checkBox2 -> setPosition ( Vec2 ( size . width * 0.6 , size . height * 0.5 ));
   
checkBox2 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
   
addChild ( checkBox2 );
   
checkBox2 -> setTag ( 2 );
   
   
// 添加文本
   
auto label2 = Text :: create ();
    label2->
setFontSize ( 20 );
    label2->
setPosition ( Vec2 ( size . width * 0.7 , size . height * 0.5 ));
    label2->
setString ( "Female" );
   
addChild (label2);
    void HelloWorld ::selectedEvent( Ref * pSender, CheckBox :: EventType type){

   
auto checkBox = ( CheckBox *)pSender;
   
int nTag =checkBox-> getTag ();
   
switch (type) {
       
case CheckBox :: EventType :: UNSELECTED :
           
if (nTag== 1 ) {
               
checkBox2 -> setSelected ( true );
               
CCLOG ( "-------1-------" );
            }
else {
               
checkBox1 -> setSelected ( true );
               
CCLOG ( "-------2-------" );
            }
           
break ;
           
       
case CheckBox :: EventType :: SELECTED :
           
if (nTag== 1 ) {
               
checkBox2 -> setSelected ( false );
               
CCLOG ( "-------3-------" );
            }
else {
               
checkBox1 -> setSelected ( false );
               
CCLOG ( "-------4-------" );
            }
           
break ;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值