使用crosswalk的setOnKeyListener问题

时间:2015-11-25 17:21:11

标签: cordova cordova-plugins crosswalk-runtime crosswalk crosswalk-project

我试图在cordova应用程序中开始使用人行横道,问题是该应用程序使用自定义插件在特定设备中使用条形码阅读器。

我们基本上有一个调用自定义View的插件类,它扩展EditText,在我们创建OnKeyListener的视图中检查特定键是否按下。

在插件类初始化期间,我们使用webView.getView().setOnKeyListener(view.getScanKeyListener());创建OnKeyListener来检查按键。

public class ScannerInputPlugin extends CordovaPlugin implements ScannerInputView.ScannerInputListener {
    private static CallbackContext callback = null;

    @Override
    public void pluginInitialize() {
        cordova.getActivity().runOnUiThread(new java.lang.Runnable() {
            public void run() {
                ScannerInputView view = new ScannerInputView(cordova.getActivity(), ScannerInputPlugin.this);
                ((ViewGroup) webView.getView()).addView(view);
                webView.getView().setOnKeyListener(view.getScanKeyListener());
            }
        });
    }

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("register")) {
            callback = callbackContext;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onScannerInput(CharSequence input) {
        if (callback != null) {
            PluginResult result = new PluginResult(PluginResult.Status.OK, input != null ? input.toString() : null);
            result.setKeepCallback(true);
            callback.sendPluginResult(result);
        }
    }
}

但是在视图中永远不会调用OnKeyListener,这只会在使用crosswalk时发生,我在调试应用时发现的唯一重大更改是webview.engine更改为XWalkWebViewEngine

public class ScannerInputView extends EditText {
    public static interface ScannerInputListener {
        void onScannerInput(CharSequence input);
    }

    public static final int KEYCODE_SCAN = 220;

    public ScannerInputView(Context context, final ScannerInputListener listener) {
        super(context);
        setLayoutParams(new ViewGroup.LayoutParams(0, 0));
        setInputType(InputType.TYPE_NULL);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (listener != null) {
                    if (count > 0 && s.charAt(start) != 0)
                        s = s.subSequence(start, start + count);
                    else
                        s = null;
                    listener.onScannerInput(s);
                }
            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    public View.OnKeyListener getScanKeyListener() {
        return new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KEYCODE_SCAN && event.getAction() == KeyEvent.ACTION_DOWN) {
                    requestFocus();
                    return true;
                }
                return false;
            }
        };
    }
}

我认为这与人行横道引擎有关,但我还没有找到原因,是否有人对此事有任何提示?

1 个答案:

答案 0 :(得分:0)

作为更新,我确实设法通过流动stackoverflow solution来解决问题,我不知道默认情况下XWalkView不可聚焦,以备将来参考问题的解决方案是setFocusable(true) ,setOnKeyListener之前的setFocusableInTouchMode(true)和requestFocus()。

public void pluginInitialize() {
    cordova.getActivity().runOnUiThread(new java.lang.Runnable() {
        public void run() {
            ScannerInputView view = new ScannerInputView(cordova.getActivity(), ScannerInputPlugin.this);
            ((ViewGroup) webView.getView()).addView(view);
            webView.getView().setFocusable(true);
            webView.getView().setFocusableInTouchMode(true);
            webView.getView().requestFocus();
            webView.getView().setOnKeyListener(view.getScanKeyListener());
        }
    });
}