简体   繁体   中英

onKeyUp not firing on EditText with Soft Keyboard shown

We have a webview that requires a keyboard. There is a bug in some Android platforms that is preventing the keyboard from showing the normal way. As a result I need to show the keyboard manually (ie from Java).

The issue is that because this is a webview, I don't have the EditText object to create the keyboard. So as a workaround I want to show the keyboard manually and pass the key inputs to the webview.

I can show the keyboard no problem, but I am unable to get the keys being touched.

I read up on intercepting onKeyDown events but I cannot get it to work.

My best attempt is this:

  1. Create my custom EditText class with an overridden onKeyUp
  2. Have the webview call this native method and have the custom EditText class request focus

My custom EditText (KeyboardText) is created but the onKeyUp is not firing. Can you advise me how exactly I can get the onKeyUp events?

Here is my custom EditText to intercept key events.

class KeyboardText extends EditText
{

    public KeyboardText(Context context)
    {

        // THIS FIRES
        super(context);
        Log.d("", "Created KeyboardText");

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {

        // THIS DOES NOT FIRE
        Log.d("","Got Key Up");

        return true;
    }

Here is where I invoke the keyboard:

        KeyboardText text = new KeyboardText(cordova.getActivity());

        text.setFocusable(true);
        text.setFocusableInTouchMode(true);
        if (text.requestFocus())
        {
            Log.d("", "Success");
            InputMethodManager manager = (InputMethodManager)cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.showSoftInput(text, InputMethodManager.SHOW_FORCED);
        }

Software keyboards rarely if ever send key events. Only hardware keys do. Software keyboards use commitText instead which is not handled by emulating it with hardware key events. If you need to interact with the keyboard, you should do so by implementing InputConnection and returning your customized InputConnection class from the focused view's getInputConnection.

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