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

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):获取文本框内提示文字

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

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);
};

然后,按需要覆写上面五个函数,使用之:
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;
}
使用CCTextFieldDelegate后,在创建好输入框后,需要把输入框通过setDelegate绑定接口,之后便可以使用。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...