bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); TextFieldTTF * tf = TextFieldTTF::textFieldWithPlaceHolder("在此输入内容", "宋体", 35); tf->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); addChild(tf); auto * lis = EventListenerTouchOneByOne::create(); //create the touch event listener (onebyone) lis->onTouchBegan = [tf](Touch *t,Event *e) { if (tf->getBoundingBox().containsPoint(t->getLocation())) { tf->attachWithIME(); } else { tf->detachWithIME(); } return false; }; Director::getInstance()->getEventDispatcher()-> addEventListenerWithSceneGraphPriority(lis, tf); return true; }