The edit box of cocos2dx edit displays a keyboard, but it is not flexible. Click return to hide it. If you need to click the background outside the keyboard to hide the keyboard, it is much easier.
Method:
1. Comment two reurns under EGLView. mm to ensure that the click event can be sent to the bottom layer when the soft keyboard is displayed.
[Cpp]
// Pass the touches to the superview
# Pragma mark EAGLView-Touch Delegate
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
[Self handleTouchesAfterKeyboardShow];
// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}
Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesBegin (I, ids, xs, ys );
}
// Pass the touches to the superview
# Pragma mark EAGLView-Touch Delegate
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
[Self handleTouchesAfterKeyboardShow];
// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}
Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesBegin (I, ids, xs, ys );
}
[Cpp]
(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
<STRONG> <SPAN style = "COLOR: # ff0000"> // WARNING: commented by Teng. Click the background to hide the soft keyboard. </SPAN>
// Return; </STRONG>
}
Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesEnd (I, ids, xs, ys );
}
-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}
Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesEnd (I, ids, xs, ys );
}
2. Add processing in the bottom layer class: Display and hide the keyboard.
[Cpp]
Void ccTouchEnded (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent)
{
Do
{
If (mTelNumber ){
CCPoint endPos = pTouch-> getLocation ();
Float delta = 5.f;
If (: abs (mBeginPos. x-endPos. x)> delta
|: Abs (mBeginPos. y-endPos. y)> delta ){
Break;
}
// Check whether the editing box is clicked
CCPoint point = mTelNumber-> getParent ()-> convertTouchToNodeSpaceAR (pTouch );
// Anchor (0.f, 0.5f)
// Int x = mTextField-> getParent ()-> getPosition (). x;
// Int y = mTextField-> getParent ()-> getPosition (). y;
Int w = mTelNumber-> getContentSize (). width;
Int h = mTelNumber-> getContentSize (). height;
CCRect rect = CCRect (0,-h/2, w, h );
OnClickedTextField (rect. containsPoint (point ));
}
} While (0 );
DialogLayer: ccTouchEnded (pTouch, pEvent );
}
/** Click the promotion code input box */
Void onClickedTextField (bool B)
{
If (B ){
MTelNumber-> attachWithIME ();
} Else {
MTelNumber-> detachWithIME ();
}
}
Void ccTouchEnded (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent)
{
Do
{
If (mTelNumber ){
CCPoint endPos = pTouch-> getLocation ();
Float delta = 5.f;
If (: abs (mBeginPos. x-endPos. x)> delta
|: Abs (mBeginPos. y-endPos. y)> delta ){
Break;
}
// Check whether the editing box is clicked
CCPoint point = mTelNumber-> getParent ()-> convertTouchToNodeSpaceAR (pTouch );
// Anchor (0.f, 0.5f)
// Int x = mTextField-> getParent ()-> getPosition (). x;
// Int y = mTextField-> getParent ()-> getPosition (). y;
Int w = mTelNumber-> getContentSize (). width;
Int h = mTelNumber-> getContentSize (). height;
CCRect rect = CCRect (0,-h/2, w, h );
OnClickedTextField (rect. containsPoint (point ));
}
} While (0 );
DialogLayer: ccTouchEnded (pTouch, pEvent );
}
/** Click the promotion code input box */
Void onClickedTextField (bool B)
{
If (B ){
MTelNumber-> attachWithIME ();
} Else {
MTelNumber-> detachWithIME ();
}
}