Cocos2d-x中提供了文本框控件CCTextFieldTTF实现文字输入功能。
CCTextFieldTTF预留了代理CCTextFieldDelegate处理通知事件。
一、CCTextFieldTTF 类部分代码如下:
- class CC_DLL CCTextFieldTTF : public CCLabelTTF,public CCIMEDelegate
- {
- public:
- /** creates a CCTextFieldTTF from a fontname,alignment,dimension and font size */
- static CCTextFieldTTF * textFieldWithPlaceHolder(const char *placeholder,const CCSize& dimensions,CCTextAlignment alignment,const char *fontName,float fontSize);
- /** creates a CCLabelTTF from a fontname and font size */
- static CCTextFieldTTF * textFieldWithPlaceHolder(const char *placeholder,float fontSize);
- /** initializes the CCTextFieldTTF with a font name,dimension and font size */
- bool initWithPlaceHolder(const char *placeholder,float fontSize);
- /** initializes the CCTextFieldTTF with a font name and font size */
- bool initWithPlaceHolder(const char *placeholder,float fontSize);
- /** Open keyboard and receive input text. */
- virtual bool attachWithIME();
- /** End text input and close keyboard.*/
- virtual bool detachWithIME();
- // input text property
- public:
- virtual void setString(const char *text);
- virtual const char* getString(void);
- // place holder text property
- // place holder text displayed when there is no text in the text field.
- public:
- virtual void setPlaceHolder(const char * text);
- virtual const char * getPlaceHolder(void);
- public:
- virtual void setSecureTextEntry(bool value);
- virtual bool isSecureTextEntry();
- };
const char* getString(void):获取文本框内容。
void setString(const char *text):设置文本框内容。
bool attachWithIME():激活输入法。
bool detachWithIME():取消激活输入法。
setPlaceHolder(const char * text):设置文本框内提示文字。
const char * getPlaceHolder(void):获取文本框内提示文字。
bool isSecureTextEntry():判断是否是安全输入。
实例:仅使用CCTextFieldTTF实现文字输入。
- CCTextFieldTTF * text = CCTextFieldTTF::textFieldWithPlaceHolder("Please Input","Arial",20);
- text->setPosition(ccp(200,200));
- text->setPlaceHolder("Enter Your Name");
- text->setString("Gong");
- text->attachWithIME();
- addChild(text);
- CCLog("%s",text->getPlaceHolder());
- CCLog("%s",text->getString());
二、CCTextFieldDelegate
CCTextFieldDelegate中部分代码如下:
- class CC_DLL CCTextFieldDelegate
- {
- public:
- /** If the sender doesn't want to attach to the IME,return true;*/
- virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * sender)
- {
- CC_UNUSED_PARAM(sender);
- return false;
- }
- /**If the sender doesn't want to detach from the IME,return true;*/
- virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * sender)
- {
- CC_UNUSED_PARAM(sender);
- return false;
- }
- /**If the sender doesn't want to insert the text,return true;*/
- virtual bool onTextFieldInsertText(CCTextFieldTTF * sender,const char * text,int nLen)
- {
- CC_UNUSED_PARAM(sender);
- CC_UNUSED_PARAM(text);
- CC_UNUSED_PARAM(nLen);
- return false;
- }
- /**If the sender doesn't want to delete the delText,return true;*/
- virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * sender,const char * delText,int nLen)
- {
- CC_UNUSED_PARAM(sender);
- CC_UNUSED_PARAM(delText);
- CC_UNUSED_PARAM(nLen);
- return false;
- }
- /**If the sender doesn't want to draw,return true.*/
- virtual bool onDraw(CCTextFieldTTF * sender)
- {
- CC_UNUSED_PARAM(sender);
- return false;
- }
- };
bool onTextFieldAttachWithIME(CCTextFieldTTF * sender):即将激活输入法,如果不想激活,返回true。
bool onTextFieldDetachWithIME(CCTextFieldTTF * sender):即将取消输入法,如果不想取消,返回true。
bool onTextFieldInsertText(CCTextFieldTTF * sender,int nLen):即将插入一段文字,如果不想插入,返回true。
bool onTextFieldDeleteBackward(CCTextFieldTTF * sender,int nLen):即将删除一段文字,如果不想删除,返回true。
bool onDraw(CCTextFieldTTF * sender):如果不希望绘制这个输入法,返回true。
实例:使用CCTextFieldDelegate实现通知相关事件
继承CCTextFieldDelegate类后需要覆写以上五个函数,按需要覆写。
首先,继承CCTextFieldDelegate:
- class HelloWorld : public cocos2d::CCLayer,public cocos2d::CCTextFieldDelegate
- {
- public:
- static cocos2d::CCScene* scene();
- virtual bool init();
- CREATE_FUNC(HelloWorld);
- bool onTextFieldAttachWithIME(CCTextFieldTTF * sender);
- bool onTextFieldDetachWithIME(CCTextFieldTTF * sender);
- bool onTextFieldInsertText(CCTextFieldTTF * sender,int nLen);
- bool onTextFieldDeleteBackward(CCTextFieldTTF * sender,int nLen);
- bool onDraw(CCTextFieldTTF * sender);
- };
然后,按需要覆写上面五个函数,使用之:
使用CCTextFieldDelegate后,在创建好输入框后,需要把输入框通过setDelegate绑定接口,之后便可以使用。
- bool HelloWorld::init()
- {
- CCLayer::init();
- CCTextFieldTTF * text = CCTextFieldTTF::textFieldWithPlaceHolder("Please Input",200));
- text->setDelegate(this);//绑定接口
- text->attachWithIME();
- addChild(text);
- return true;
- }
- bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF * sender)
- {
- return false;
- }
- bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF * sender)
- {
- //将屏幕上移,避免虚拟键盘遮挡输入框
- this->setPosition(0,100);
- return false;
- }
- bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF * sender,int nLen)
- {
- //将屏幕移动会原处
- this->setPosition(ccp(0,0));
- return false;
- }
- bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF * sender,int nLen)
- {
- return false;
- }
- bool HelloWorld::onDraw(CCTextFieldTTF * sender)
- {
- return false;
- }