加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

我的Cocos2d-x学习笔记(二十二)CCTextFieldTTF (文字输入)、

发布时间:2020-12-14 21:27:20 所属栏目:百科 来源:网络整理
导读:Cocos2d-x中提供了文本框控件CCTextFieldTTF实现文字输入功能。 CCTextFieldTTF预留了代理CCTextFieldDelegate处理通知事件。 一、CCTextFieldTTF 类部分代码如下: class CC_DLL CCTextFieldTTF : public CCLabelTTF,public CCIMEDelegate{public: /** crea

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绑定接口,之后便可以使用。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读