从零开始学习cocoStudio(7)--登录

登录


1.描述


      用户登录功能即对用户输入的账号、密码以进行验证。如图:




2.代码:

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    m_pUILayer = UILayer::create();
    m_pUILayer->scheduleUpdate();
    addChild(m_pUILayer);
    
    //注册根节点
    m_pLayout = dynamic_cast<UILayout*>(GUIReader::shareReader()->widgetFromJsonFile("cocosgui/gui_examples/DemoLogin/DemoLogin.json"));
    m_pUILayer->addWidget(m_pLayout);
    
    UITextField* comfirm_textfield = static_cast<UITextField*>(m_pUILayer->getWidgetByName("confirm_TextField"));
    comfirm_textfield->addEventListenerTextField(this, textfieldeventselector(HelloWorld::textFieldEvent));
    
    // close button
    UIButton* close_button = static_cast<UIButton*>(m_pUILayer->getWidgetByName("close_Button"));
    close_button->addTouchEventListener(this, toucheventselector(HelloWorld::menuCloseCallback));
    
    return true;
}


void HelloWorld::textFieldEvent(CCObject *pSender, TextFiledEventType type)
{
    switch (type)
    {
        case TEXTFIELD_EVENT_ATTACH_WITH_IME:
        {
            UITextField* textField = dynamic_cast<UITextField*>(pSender);
            
            if (strcmp(textField->getName(), "confirm_TextField") == 0)
            {
                CCMoveBy* moveBy = CCMoveBy::create(0.1f, ccp(0, textField->getContentSize().height * 2.5));
                m_pLayout->runAction(moveBy);
            }
            CCLog("点击");
        }
            break;
            
        case TEXTFIELD_EVENT_DETACH_WITH_IME:
        {
            UITextField* textField = dynamic_cast<UITextField*>(pSender);
            
            if (strcmp(textField->getName(), "confirm_TextField") == 0)
            {
                CCMoveBy* moveBy = CCMoveBy::create(0.1f, ccp(0, -textField->getContentSize().height * 2.5));
                m_pLayout->runAction(moveBy);
            }
            
            CCLog("移开");
            printf("%s",textField->getStringValue());
        }
            break;
            
        case TEXTFIELD_EVENT_INSERT_TEXT:
            CCLog("增加字段");
                
            break;
            
        case TEXTFIELD_EVENT_DELETE_BACKWARD:
            CCLog("删除字段");
            break;
            
        default:
            break;
    }

}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
注释:UItextField通常用于外部数据输入,以实现人机交互。事件处理如下:

typedef enum
{
    TEXTFIELD_EVENT_ATTACH_WITH_IME,     //点击
    TEXTFIELD_EVENT_DETACH_WITH_IME,     //移除
    TEXTFIELD_EVENT_INSERT_TEXT,	 //增加字段
    TEXTFIELD_EVENT_DELETE_BACKWARD,     //删除字段
}TextFiledEventType;

如上,只有介绍前端控件基本使用方法,若想深入理解下登录功能的话,可参考下该篇博文:http://www.cnblogs.com/coolhwm/archive/2011/11/19/2255116.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值