EditBox样例
将例如以下代码拷贝到新建的project中就能看到效果。
HelloWorldScene.h
#include "cocos2d.h" /*这里要引入头文件*/ #include "extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld : public cocos2d::Layer ,public EditBoxDelegate { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); CREATE_FUNC(HelloWorld); virtual void editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox); virtual void editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox); virtual void editBoxTextChanged(cocos2d::extension::EditBox* editBox, const std::string& text); virtual void editBoxReturn(cocos2d::extension::EditBox* editBox); void toExtensionsMainLayer(cocos2d::Ref *sender); private: cocos2d::Label* _TTFShowEditReturn; cocos2d::extension::EditBox* _editName; cocos2d::extension::EditBox* _editPassword; cocos2d::extension::EditBox* _editEmail; bool _drag; };
HelloWorldScene.cpp
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } auto glview = Director::getInstance()->getOpenGLView(); auto visibleOrigin = glview->getVisibleOrigin(); CCLOG("%f",visibleOrigin.x); auto visibleSize = glview->getVisibleSize(); auto pBg = Sprite::create("HelloWorld.png"); pBg->setPosition(Point(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2)); addChild(pBg); _TTFShowEditReturn = Label::createWithSystemFont("No edit control return!", "", 30); _TTFShowEditReturn->setPosition(Point(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height - 50)); addChild(_TTFShowEditReturn); //Back Menu auto itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(HelloWorld::toExtensionsMainLayer, this)); itemBack->setPosition(Point(visibleOrigin.x + visibleSize.width - 50, visibleOrigin.y + 25)); auto menuBack = Menu::create(itemBack, NULL); menuBack->setPosition(Point::ZERO); addChild(menuBack); auto editBoxSize = Size(visibleSize.width - 100, 60); _editName = EditBox::create(editBoxSize, Scale9Sprite::create("green_edit.png")); _editName->setPosition(Point(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height * 3 / 4)); _editName->setFontName("Paint Boy"); _editName->setFontSize(25); _editName->setFontColor(Color3B::RED); _editName->setPlaceHolder("Name:"); _editName->setPlaceholderFontColor(Color3B::WHITE); _editName->setMaxLength(8); _editName->setReturnType(EditBox::KeyboardReturnType::DONE); _editName->setDelegate(this); addChild(_editName); _editPassword = EditBox::create(editBoxSize, Scale9Sprite::create("orange_edit.png")); _editPassword->setPosition(Point(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) _editPassword->setFont("American Typewriter", 30); #else _editPassword->setFont("fonts/American Typewriter.ttf", 30); #endif _editPassword->setFontColor(Color3B::GREEN); _editPassword->setPlaceHolder("Password:"); _editPassword->setMaxLength(6); _editPassword->setInputFlag(EditBox::InputFlag::PASSWORD); _editPassword->setInputMode(EditBox::InputMode::SINGLE_LINE); _editPassword->setDelegate(this); addChild(_editPassword); //bottom _editEmail = EditBox::create(Size(editBoxSize.width, editBoxSize.height), Scale9Sprite::create("yellow_edit.png")); _editEmail->setPosition(Point(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 4)); _editEmail->setAnchorPoint(Point(0.5f, 1.0f)); _editEmail->setPlaceHolder("Email:"); _editEmail->setInputMode(EditBox::InputMode::EMAIL_ADDRESS); _editEmail->setDelegate(this); addChild(_editEmail); return true; } void HelloWorld::toExtensionsMainLayer(cocos2d::Ref *sender) { } void HelloWorld::editBoxEditingDidBegin(cocos2d::extension::EditBox *editBox) { log("editBox %p DidBegin !", editBox); } void HelloWorld::editBoxEditingDidEnd(cocos2d::extension::EditBox *editBox) { log("editBox %p DidEnd !", editBox); } void HelloWorld::editBoxTextChanged(cocos2d::extension::EditBox *editBox, const std::string &text) { log("editBox %p TextChanged, text: %s ", editBox, text.c_str()); } void HelloWorld::editBoxReturn(cocos2d::extension::EditBox *editBox) { log("editBox %p was returned !",editBox); if (_editName == editBox) { _TTFShowEditReturn->setString("Name EditBox return !"); }else if (_editPassword == editBox) { _TTFShowEditReturn->setString("Password EditBox return !"); }else if (_editEmail == editBox) { _TTFShowEditReturn->setString("Email EditBox return !"); } }
效果: