简体   繁体   English

使用软键盘显示时,onKeyUp无法在EditText上触发

[英]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. 在某些Android平台中存在一个错误,导致键盘无法正常显示。 As a result I need to show the keyboard manually (ie from Java). 结果,我需要手动显示键盘(即从Java显示)。

The issue is that because this is a webview, I don't have the EditText object to create the keyboard. 问题是,因为这是一个Web视图,所以我没有EditText对象来创建键盘。 So as a workaround I want to show the keyboard manually and pass the key inputs to the webview. 因此,作为一种解决方法,我想手动显示键盘并将按键输入传递到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. 我阅读了有关侦听onKeyDown事件的信息,但无法使其正常工作。

My best attempt is this: 我最好的尝试是这样的:

  1. Create my custom EditText class with an overridden onKeyUp 使用覆盖的onKeyUp创建我的自定义EditText类
  2. Have the webview call this native method and have the custom EditText class request focus 让webview调用此本机方法,并使自定义EditText类请求焦点

My custom EditText (KeyboardText) is created but the onKeyUp is not firing. 我的自定义EditText(KeyboardText)已创建,但onKeyUp没有触发。 Can you advise me how exactly I can get the onKeyUp events? 您能告诉我如何准确地获取onKeyUp事件吗?

Here is my custom EditText to intercept key events. 这是我自定义的EditText来拦截关键事件。

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. 软件键盘使用commitText代替,而不会通过使用硬件按键事件模拟​​来处理commitText。 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. 如果需要与键盘进行交互,则应通过实现InputConnection并从焦点视图的getInputConnection返回定制的InputConnection类来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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