如何实现输入文本TextFiledTTF
如何实现输入文本TextFiledTTF
1 //获得可见区域的大小 2 Size visibleSize = Director::getInstance()->getVisibleSize(); 3 4 //使用一个默认要先呈现一段文字 5 TextFieldTTF *tf = TextFieldTTF::textFieldWithPlaceHolder("在这里输入", "宋体", 20); 6 7 tf->setPosition(visibleSize.width/2, visibleSize.height/2);//设置显示的位置 8 9 addChild(tf); 10 11 //将输入文本与输入法链接 12 //添加监听器 13 auto listener = EventListenerTouchOneByOne::create(); 14 listener->onTouchBegan = [tf](Touch *t, Event *event){//闭包函数 15 //先获取文本狂的边界区域.是否包括触摸点(Touch对象) 16 if (tf->getBoundingBox().containsPoint(t->getLocation())) { 17 tf->attachWithIME();//链接输入法 18 19 log(">><><>"); 20 }else{ 21 tf->detachWithIME(); 22 } 23 24 return false; 25 }; 26 //添加监听器 27 Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, tf);