我的Cocos2d-x学习笔记(二十二)CCTextFieldTTF (文字输入)、CCTextFieldDelegate(输入通知事件)

前端之家收集整理的这篇文章主要介绍了我的Cocos2d-x学习笔记(二十二)CCTextFieldTTF (文字输入)、CCTextFieldDelegate(输入通知事件)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2d-x中提供了文本框控件CCTextFieldTTF实现文字输入功能

CCTextFieldTTF预留了代理CCTextFieldDelegate处理通知事件。

一、CCTextFieldTTF 类部分代码如下:

  1. class CC_DLL CCTextFieldTTF : public CCLabelTTF,public CCIMEDelegate
  2. {
  3. public:
  4. /** creates a CCTextFieldTTF from a fontname,alignment,dimension and font size */
  5. static CCTextFieldTTF * textFieldWithPlaceHolder(const char *placeholder,const CCSize& dimensions,CCTextAlignment alignment,const char *fontName,float fontSize);
  6. /** creates a CCLabelTTF from a fontname and font size */
  7. static CCTextFieldTTF * textFieldWithPlaceHolder(const char *placeholder,float fontSize);
  8. /** initializes the CCTextFieldTTF with a font name,dimension and font size */
  9. bool initWithPlaceHolder(const char *placeholder,float fontSize);
  10. /** initializes the CCTextFieldTTF with a font name and font size */
  11. bool initWithPlaceHolder(const char *placeholder,float fontSize);
  12.  
  13. /** Open keyboard and receive input text. */
  14. virtual bool attachWithIME();
  15.  
  16. /** End text input and close keyboard.*/
  17. virtual bool detachWithIME();
  18.  
  19. // input text property
  20. public:
  21. virtual void setString(const char *text);
  22. virtual const char* getString(void);
  23.  
  24. // place holder text property
  25. // place holder text displayed when there is no text in the text field.
  26. public:
  27. virtual void setPlaceHolder(const char * text);
  28. virtual const char * getPlaceHolder(void);
  29.  
  30. public:
  31. virtual void setSecureTextEntry(bool value);
  32. virtual bool isSecureTextEntry();
  33. };

const char* getString(void):获取文本框内容

void setString(const char *text):设置文本框内容

bool attachWithIME():激活输入法。

bool detachWithIME():取消激活输入法。

setPlaceHolder(const char * text):设置文本框内提示文字

const char * getPlaceHolder(void):获取文本框内提示文字

void setSecureTextEntry(bool value):设置安全输入模式,输入字符显示*。

bool isSecureTextEntry():判断是否是安全输入。

实例:仅使用CCTextFieldTTF实现文字输入。

  1. CCTextFieldTTF * text = CCTextFieldTTF::textFieldWithPlaceHolder("Please Input","Arial",20);
  2. text->setPosition(ccp(200,200));
  3. text->setPlaceHolder("Enter Your Name");
  4. text->setString("Gong");
  5. text->attachWithIME();
  6. addChild(text);
  7. CCLog("%s",text->getPlaceHolder());
  8. CCLog("%s",text->getString());


二、CCTextFieldDelegate

CCTextFieldDelegate中部分代码如下:

  1. class CC_DLL CCTextFieldDelegate
  2. {
  3. public:
  4. /** If the sender doesn't want to attach to the IME,return true;*/
  5. virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * sender)
  6. {
  7. CC_UNUSED_PARAM(sender);
  8. return false;
  9. }
  10.  
  11. /**If the sender doesn't want to detach from the IME,return true;*/
  12. virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * sender)
  13. {
  14. CC_UNUSED_PARAM(sender);
  15. return false;
  16. }
  17.  
  18. /**If the sender doesn't want to insert the text,return true;*/
  19. virtual bool onTextFieldInsertText(CCTextFieldTTF * sender,const char * text,int nLen)
  20. {
  21. CC_UNUSED_PARAM(sender);
  22. CC_UNUSED_PARAM(text);
  23. CC_UNUSED_PARAM(nLen);
  24. return false;
  25. }
  26.  
  27. /**If the sender doesn't want to delete the delText,return true;*/
  28. virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * sender,const char * delText,int nLen)
  29. {
  30. CC_UNUSED_PARAM(sender);
  31. CC_UNUSED_PARAM(delText);
  32. CC_UNUSED_PARAM(nLen);
  33. return false;
  34. }
  35.  
  36. /**If the sender doesn't want to draw,return true.*/
  37. virtual bool onDraw(CCTextFieldTTF * sender)
  38. {
  39. CC_UNUSED_PARAM(sender);
  40. return false;
  41. }
  42. };

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:

  1. class HelloWorld : public cocos2d::CCLayer,public cocos2d::CCTextFieldDelegate
  2. {
  3. public:
  4.  
  5. static cocos2d::CCScene* scene();
  6. virtual bool init();
  7. CREATE_FUNC(HelloWorld);
  8. bool onTextFieldAttachWithIME(CCTextFieldTTF * sender);
  9. bool onTextFieldDetachWithIME(CCTextFieldTTF * sender);
  10. bool onTextFieldInsertText(CCTextFieldTTF * sender,int nLen);
  11. bool onTextFieldDeleteBackward(CCTextFieldTTF * sender,int nLen);
  12. bool onDraw(CCTextFieldTTF * sender);
  13. };

然后,按需要覆写上面五个函数,使用之:
  1. bool HelloWorld::init()
  2. {
  3. CCLayer::init();
  4.  
  5. CCTextFieldTTF * text = CCTextFieldTTF::textFieldWithPlaceHolder("Please Input",200));
  6. text->setDelegate(this);//绑定接口
  7. text->attachWithIME();
  8.  
  9. addChild(text);
  10. return true;
  11. }
  12. bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF * sender)
  13. {
  14.  
  15. return false;
  16. }
  17. bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF * sender)
  18. {
  19. //将屏幕上移,避免虚拟键盘遮挡输入框
  20. this->setPosition(0,100);
  21. return false;
  22. }
  23. bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF * sender,int nLen)
  24. {
  25. //将屏幕移动会原处
  26. this->setPosition(ccp(0,0));
  27. return false;
  28. }
  29. bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF * sender,int nLen)
  30. {
  31.  
  32. return false;
  33. }
  34. bool HelloWorld::onDraw(CCTextFieldTTF * sender)
  35. {
  36.  
  37. return false;
  38. }
使用CCTextFieldDelegate后,在创建好输入框后,需要把输入框通过setDelegate绑定接口,之后便可以使用。

猜你在找的Cocos2d-x相关文章