[Code snippet] CCEditBox on android hides with one back button press and keyboard height


#1

Hi,

i’ve made this modification to the onCreate() method Cocos2dxEditDialog.java so the keyboard hides when the user pressed the back button, instead of having to press 2 times:

     this.mInputEditText = new EditText(this.getContext())
        {
            @Override
            public boolean onKeyPreIme(int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && 
                    event.getAction() == KeyEvent.ACTION_UP) {
                    Cocos2dxHelper.setEditTextDialogResult(Cocos2dxEditBoxDialog.this.mInputEditText.getText().toString());
                    Cocos2dxEditBoxDialog.this.closeKeyboard();
                    Cocos2dxEditBoxDialog.this.dismiss();
                    return true;
                }

                return super.onKeyPreIme(keyCode, event);
            }
        };

This is basically inheriting EditText and overriding the onKeyPreIme method. You could inherit it elsewhere and have a better code, but this is easier to share on the forum :stuck_out_tongue:

EDIT: I’ve also found how we can find out the keyboard height, so we could do like iphone, move the view up and show the in-game field instead of the android native view:

Cheers!


#2

so we could do like iphone, move the view up and show the in-game field instead of the android native view
Do you know how to do this? I really like that idea


#3

Justin Godesky wrote:

Do you know how to do this? I really like that idea

I have not implemented it, but if you take a look at keyboardWillShow in EditBox you’ll see that it gets a CCIMEKeyboardNotificationInfo object by parameter.

There are some problems:
# keyboardWillShow METHOD IS NOT CALLED ON ANDROID I think it should be called on Cocos2dxEditBoxDialog.java through JNI. You could call a helper method that just receieves floats so it’s easier to implement the JNI part.
# You’ll need to fill the CCIMEKeyboardNotificationInfo with keyboard dimensions.

If you manage to sort this problems out it shouldn’t be too hard to make it work.

If you or anybody is interested in making the android CCEditBox look like the iOS one we should:

  • Get rid of the text field of the top of the screen or move it to the top of the keyboard.
  • We could try to remove that view and and send the keyboard strokes directly to the CCEditBox beign drawn in-game. (Could be done like this: http://developer.android.com/training/keyboard-input/commands.html ? Seems a bit overcomplicated, maybe.)