如何在关闭包含我的TextField的图层时隐藏键盘

时间:2015-02-05 06:03:36

标签: cocos2d-x textfield cocos2d-x-3.0

我在我的新项目中使用TextField,它位于cocos2d-x 3.2中。当我输入文本然后输入然后输入然后它隐藏我的键盘但是当我输入文本&如果我关闭包含文本字段的PopUp图层,那么我的键盘会在那时打开,然后我的键盘仍然打开。在这种情况下,我不知道如何隐藏或关闭键盘。请帮忙......

这是我的代码..

//在我的图层中添加TextField

TextFiledd = TextField::create("", "fonts/HelveticaLTStd-Cond_0.ttf" , ButtonFontSize);
TextFiledd->setContentSize(Size(Playername_bg->getContentSize().width ,Playername_bg->getContentSize().height));
TextFiledd->setPosition(Point(Playername_bg->getPositionX(),Playername_bg->getPositionY()));
TextFiledd->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
TextFiledd->setMaxLength(10);
TextFiledd->setMaxLengthEnabled(true);
TextFiledd->addEventListener(CC_CALLBACK_2(PlayerName::textFieldEvent, this));
TextFiledd->setColor(Color3B::YELLOW);

TextFiledd->setBright(true);

TextFiledd->setTouchAreaEnabled(true);
TextFiledd->setTouchSize(Playername_bg->getContentSize());

bgFrameSprite->addChild(TextFiledd,PopUpTag);

//委托方法

 void PlayerName::textFieldEvent(cocos2d::Ref *pSender, TextField::EventType type)
 {
playerNameText = (TextField*)pSender;
CCLOG(" Player name : %s",playerNameText->getStringValue().c_str());
std::string key = Player_Name;
PlayerSettings::setPlayerName(key.c_str(), playerNameText->getStringValue().c_str());

switch (type)
{
    case TextField::EventType::ATTACH_WITH_IME:
    {
        TextField* textField = dynamic_cast<TextField*>(pSender);
        Size widgetSize = WinSize;
        runAction(CCMoveTo::create(0.225f,Vec2(0, widgetSize.height / 12.0f)));
        textField->setTextHorizontalAlignment(TextHAlignment::LEFT);
        textField->setTextVerticalAlignment(TextVAlignment::TOP);     
        playerNameText->setText(str);
    }
        break;

    case TextField::EventType::DETACH_WITH_IME:
    {
        TextField* textField = dynamic_cast<TextField*>(pSender);
        Size widgetSize = WinSize;
        runAction(CCMoveTo::create(0.175f, Vec2(0, 0)));
        textField->setTextHorizontalAlignment(TextHAlignment::LEFT);
        textField->setTextVerticalAlignment(TextVAlignment: 
    }
        break;

    case TextField::EventType::INSERT_TEXT:
        break;

    case TextField::EventType::DELETE_BACKWARD:
        break;

    default:
        break;
  }
 }

1 个答案:

答案 0 :(得分:2)

这对我有用(cocos2d-x-3.11.1):

textField->detachWithIME()

在iOS和Android上。