【10】coco2d-x CCTextFieldTTF最简单的方法实现密码登陆“*”

本文介绍如何使用Cocos2d-x中的CCTextFieldTTF组件来实现图密码框的功能。通过覆盖几个关键的CCTextFieldDelegate方法,可以实现在输入密码时用星号代替实际字符,并在完成输入后显示真实密码。

最简单的方法使用CCTextFieldTTF设置图密码框,

首先类的继承关系

class Login:public cocos2d::CCLayer,CCIMEDelegate,CCTextFieldDelegate

最有是这几个方法

	// CCTextFieldDelegate
	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);
	virtual bool onDraw(CCTextFieldTTF * pSender);


先在这个bool Login::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen)方向里加入以下代码

int count=((pSender->getCharCount())>0?(pSender->getCharCount()):1);
if (count<=0)
{
count=1;
}
std::string paSt(count,'*');
pSender->setString(paSt.c_str());

这样,每输入一个字符,上一个字符就变成了“*”

输入接收后,这这个bool Login::onTextFieldDetachWithIME(CCTextFieldTTF * pSender)方法里

std::string pstr=pSender->getPlaceHolder();
if (pstr=="password...")
{
std::string *inpstr=pSender->m_pInputText;
std::string showstr(inpstr->c_str(),0,inpstr->length()-1);
pSender->setString(showstr.c_str());
}

这样就全部显示成了“*”

注意这里pSender->m_pInputText;会报错了,进入CCTextFieldTTF.h,将m_pInputText该成public的就OK了!


本方法也许会有问题!因为修改了框架源码!但是我暂时没有发现!本人也在学习中,发现问题的希望大家留意讨论!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值