登录
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