简体   繁体   中英

Android onKeyUp doesn't work in Activity

Soft keyboard doesn't work in my activity? But after press home button or any system UI button, excepts back button its starting work normally.

@Override
public boolean onKeyUp(int keyCode, final KeyEvent event) {
    final int finalKeyCode = keyCode;
    View lView = mParent.lET.findFocus();
    if(lView == mParent.lET)
    {
        if(keyCode == KeyEvent.KEYCODE_ENTER)
        {
            this.mGLThread.androidHideSoftKeyboard();
        }
        else
        {
            mParent.lET.bringToFront();
            mParent.lET.onKeyUp(finalKeyCode, event);
            mPlayerName = mParent.lET.getText().toString();
        }
    }

    return false;
}

Hardware buttons triggers this function, but soft keyboard didn't work. Thanks.

onKeyUp is supposed to handle hardware keys not the soft keys. So you can not handle key press of soft keyboard like this. To do this you can do one thing. on the EditText set TextChangedListener. Sample Code

edit.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {

            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){

            }
            public void onTextChanged(CharSequence s, int start, int before, int count){

            }

        });

onKeyListener worked perfectly on Android 1.5 via the soft keyboard

From Android 1.6 onwards the character and number keys are not going via the onKey event, yet the DEL key does

Give this a try

Set the listener you defined to your EditText

edittext.setOnEditorActionListener(new HideYourKeypad());

Define your listener

    // Added try-catch just in case JellyBean has any other lurking errors
public class HideYourKeypad implements OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView view, int actionId,
            KeyEvent event) {
        try {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null && view != null) {
                switch (actionId) {
                case EditorInfo.IME_NULL:
                    if ((event == null)
                            || (event.getAction() == KeyEvent.ACTION_DOWN))
                        imm.hideSoftInputFromWindow(view.getWindowToken(),
                                0);
                    return true;

                case EditorInfo.IME_ACTION_GO:
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    return true;

                case EditorInfo.IME_ACTION_DONE:
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    return true;
                }
            }
        } catch (Throwable x) {
            Logger.warning(TAG, "Error hiding keyboard", x);
        }

        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM