Hi,
I’ve got a text field on screen which on iOS opens a keyboard.
Text field is within a popup, so in onTextFieldAttachWithIME I move it up a bit and in onTextFieldDetachWithIME I move it down.
When running on iOS everything seems fine, however when I run it on Android device keyboard does not show up.
Popup moves a bit to the top, but keyboard is not there.
What could be wrong?
I’ve basically copied and pasted code of the cctextfieldttf test and used the default text field [+added delegate to support the methods mentioned above].
Any help will be appreciated.
Thanks,
Krystian
Here’s the code for my input box:
// TEXT INPUT
//////////////////////////////////////////////////////////////////////////
// implement KeyboardNotificationLayer
//////////////////////////////////////////////////////////////////////////
KeyboardNotificationLayer::KeyboardNotificationLayer()
: m_pTrackNode(0) {
setIsTouchEnabled(true);
}
void KeyboardNotificationLayer::registerWithTouchDispatcher() {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false);
}
void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info) {
CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)",
info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height);
if (!m_pTrackNode) {
return;
}
CCRect rectTracked = getRect(m_pTrackNode);
CCLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)",
rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height);
// if the keyboard area doesn't intersect with the tracking node area, nothing need to do.
if (!CCRect::CCRectIntersectsRect(rectTracked, info.end)) {
return;
}
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
float adjustVert = CCRect::CCRectGetMaxY(info.end) - CCRect::CCRectGetMinY(rectTracked);
CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert);
// move all the children node of KeyboardNotificationLayer
CCArray *children = getChildren();
CCNode *node = 0;
int count = children->count();
CCPoint pos;
for (int i = 0; i < count; ++i) {
node = (CCNode *) children->objectAtIndex(i);
pos = node->getPosition();
pos.y += adjustVert;
node->setPosition(pos);
}
}
// CCLayer function
bool KeyboardNotificationLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
CCLOG("++++++++++++++++++++++++++++++++++++++++++++");
m_beginPos = pTouch->locationInView(pTouch->view());
m_beginPos = CCDirector::sharedDirector()->convertToGL(m_beginPos);
return true;
}
void KeyboardNotificationLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
if (!m_pTrackNode) {
return;
}
CCPoint endPos = pTouch->locationInView(pTouch->view());
endPos = CCDirector::sharedDirector()->convertToGL(endPos);
float delta = 5.0f;
if (::abs(endPos.x - m_beginPos.x) > delta
|| ::abs(endPos.y - m_beginPos.y) > delta) {
// not click
m_beginPos.x = m_beginPos.y = -1;
return;
}
// decide the trackNode is clicked.
CCRect rect;
CCPoint point = convertTouchToNodeSpaceAR(pTouch);
CCLOG("KeyboardNotificationLayer:clickedAt(%f,%f)", point.x, point.y);
rect = getRect(m_pTrackNode);
CCLOG("KeyboardNotificationLayer:TrackNode at(origin:%f,%f, size:%f,%f)",
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
this->onClickTrackNode(CCRect::CCRectContainsPoint(rect, point));
CCLOG("----------------------------------");
}
//////////////////////////////////////////////////////////////////////////
// implement TextFieldTTFDefaultTest
//////////////////////////////////////////////////////////////////////////
std::string TextFieldTTFDefaultTest::subtitle() {
return "TextFieldTTF with default behavior test";
}
void TextFieldTTFDefaultTest::onClickTrackNode(bool bClicked) {
CCTextFieldTTF *pTextField = (CCTextFieldTTF *) m_pTrackNode;
if (bClicked) {
// TextFieldTTFTest be clicked
CCLOG("TextFieldTTFDefaultTest:CCTextFieldTTF attachWithIME");
pTextField->attachWithIME();
}
else {
// TextFieldTTFTest not be clicked
CCLOG("TextFieldTTFDefaultTest:CCTextFieldTTF detachWithIME");
pTextField->detachWithIME();
}
}
void TextFieldTTFDefaultTest::onEnter() {
KeyboardNotificationLayer::onEnter();
// add CCTextFieldTTF
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCTextFieldTTF *pTextField = CCTextFieldTTF::textFieldWithPlaceHolder("",
FONT_NAME,
FONT_SIZE);
pTextField->setPosition(ogsPosition(ccp(0, -30), ogsAnchorCenter));
addChild(pTextField);
pTextField->setDelegate(this);
m_pTrackNode = pTextField;
}
std::string TextFieldTTFDefaultTest::getString() {
return ((CCTextFieldTTF*) m_pTrackNode)->getString();
}
bool TextFieldTTFDefaultTest::onTextFieldAttachWithIME (CCTextFieldTTF *sender) {
this->getParent()->setPosition(ccpAdd(this->getParent()->getPosition(), ogsPosition(ccp(0, 100), ogsAnchorBottomLeft)));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//on ios field will move itself, so we have to compensate
this->setPosition(ogsPosition(ccp(0, -50), ogsAnchorBottomLeft));
#endif
return false;
}
bool TextFieldTTFDefaultTest::onTextFieldDetachWithIME (CCTextFieldTTF *sender) {
this->getParent()->setPosition(ccpAdd(this->getParent()->getPosition(), ogsPosition(ccp(0, -100), ogsAnchorBottomLeft)));
return false;
}
bool TextFieldTTFDefaultTest::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen) {
// if insert enter, treat as default to detach with ime
if ('\n' == *text) {
return false;
}
// if the textfield's char count more than m_nCharLimit, doesn't insert text anymore.
if (pSender->getCharCount() >= m_nCharLimit) {
return true;
}
return false;
}
and header:
//////////////////////////////////////////////////////////////////////////
// KeyboardNotificationLayer for test IME keyboard notification.
//////////////////////////////////////////////////////////////////////////
class KeyboardNotificationLayer : public CCLayer, public CCIMEDelegate
{
public:
KeyboardNotificationLayer();
virtual std::string subtitle() = 0;
virtual void onClickTrackNode(bool bClicked) = 0;
virtual void registerWithTouchDispatcher();
virtual void keyboardWillShow(CCIMEKeyboardNotificationInfo& info);
// CCLayer
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
protected:
CCNode * m_pTrackNode;
CCPoint m_beginPos;
};
//////////////////////////////////////////////////////////////////////////
// TextFieldTTFDefaultTest for test TextFieldTTF default behavior.
//////////////////////////////////////////////////////////////////////////
class TextFieldTTFDefaultTest : public KeyboardNotificationLayer, public CCTextFieldDelegate
{
public:
int m_nCharLimit;
std::string getString();
// KeyboardNotificationLayer
virtual std::string subtitle();
virtual void onClickTrackNode(bool bClicked);
virtual bool onTextFieldAttachWithIME (CCTextFieldTTF *sender);
virtual bool onTextFieldDetachWithIME (CCTextFieldTTF *sender);
virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen);
// CCLayer
virtual void onEnter();
};