一、CCTextFieldTTF创建与使用
1、CCTextFieldTTF::create(const char* placeholder,const char* fontName,float fontSize)
参数1:默认显示的文本;
参数2:字体名称;
参数3:字体大小
除了getString与setString两个函数之外,还有两个重要函数:
(1)attachWithIME():此函数是输入开关,当CCTextFieldTTF函数被调用后,Cocos2d-x会默认调出当前设备的虚拟键盘。
(2)detachWithIME():关闭虚拟键盘。
2、CCTextFieldDelegate:通过接口提供的回调函数进行读写,从而达到时刻监控CCTextFieldTTF状态变化的目标。具体步骤:
(1)当前类使用CCTextFieldDelegate接口,代码如下:
class HelloWorld:public cocos2d::CCLayer,public
cocos2d::CCTextFieldDelegate{……}
(2)重写回调函数,代码如下:
virtual bool onTextFieldAttachWithIME(CCTextFieldTTF* pSender);//当用户启动虚拟键盘时的回调函数
virtual bool onTextFieldDetachWithIME(CCTextFieldTTF* pSender);//当用户关闭虚拟键盘时的回调函数
virtual bool onTextFieldInsertText(CCTextFieldTTF* pSender,const char *text,int nLen);//当用户进行输入的回调函数
virtual bool onTextFieldDeleteBackward(CCTextFieldTTF* pSender,const char *delText,int nLen);//当用户删除文字时的回调函数。
(3)在cpp文件中实现重写的回调函数
(4)调用CCTextFieldTTF的setDelegate(CCTextFieldDelegate* pDelegate)函数绑定接口。