用法如下:
1.类继承publiccocos2d::extension::CCEditBoxDelegate,并实现CCEditBox需要实现的代理
virtualvoideditBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox);
virtualvoideditBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox);
virtualvoideditBoxTextChanged(cocos2d::extension::CCEditBox* editBox,conststd::string& text);
virtualvoideditBoxReturn(cocos2d::extension::CCEditBox* editBox);
2.定义输入框
cocos2d::extension::CCEditBox* mEditName;
mEditName =SUIMgr.createCEditBox("EditAccount",this,400,250,60);
mEditName->setPosition(middleParentNode->getPosition());
mEditName->setPlaceholderFontColor(ccc3(255,255,255));
mEditName->setText("请输入名字");
mEditName->setMaxLength(10);
this->addChild(mEditName);
voideditBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox)
{
mEditName->setText("");
mStrName ="";
}
voideditBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox){
}
voideditBoxTextChanged(cocos2d::extension::CCEditBox* editBox,const std::string& text){
mStrName = text;
}
voideditBoxReturn(cocos2d::extension::CCEditBox* editBox){
}
4.检测函数(输入完成后调用,或向服务器请求时候调用,自检)
voidcheckNameNomative(conststring& text)
{
if (text =="") {
CCMessageBox("名字不能为空","account error!");
return;
}
//在苹果环境下需转为宽字符,因一个string中文长度为3,宽字符为1
std::wstring name_wstring =Utils::s2ws(text);
int chiniseSize =0;
int engnishSize =0;
for (int i =0; i != name_wstring.length(); i++) {
// 判断是否为英文字母
if (((name_wstring[i] >=48) && (name_wstring[i]<=57)) || ((name_wstring[i] >=65) && (name_wstring[i] <=90)) || ((name_wstring[i] >=97) && (name_wstring[i] <=122))) {
engnishSize ++;
}
//是否为汉字
if (name_wstring[i] >127) {
chiniseSize++;
}
}
// 全部为汉字1~6
if (((chiniseSize >0) && (chiniseSize < 7)) && (engnishSize ==0)) {
CCLOG("全部汉字:%d",chiniseSize);
}
// 全部英文2~10
elseif (((engnishSize >1) && (engnishSize <11)) && (chiniseSize == 0)){
CCLOG("全部英文:%d",engnishSize);
}
// 汉字加英文1~6
elseif (((chiniseSize >0) && (chiniseSize <7)) && ((engnishSize > 0) && (engnishSize <11)) && (chiniseSize + engnishSize <7)) {
CCLOG("汉字加英文:%d + %d",chiniseSize,33)"> // 长度错误
else {
CCLOG("长度错误!%d + %d",engnishSize);
}
}
// string 转宽字符函数 注:utf8是一开源文件,其中有包含检测的代码。用时需导入相关几个文件
std::wstringUtils::s2ws(conststd::string& s)
{
std::wstring resultstring;
resultstring.reserve(s.length());
utf8::utf8to16(s.begin(),s.end(),std::back_inserter(resultstring));
return resultstring;
}
最后,cceditBox未做中英文区分,需自己写检测方法,用此方法可实现检测以及达到中英文输入自量的不同。
来自:http://blog.csdn.net/u010619611/article/details/36391455 原文链接:https://www.f2er.com/cocos2dx/339014.html