Cordova setOnKeyListener使用人行横道时出现问题
我正在尝试在cordova应用程序中使用crosswalk,问题是该应用程序在特定设备中为条形码阅读器使用自定义插件 我们基本上有一个插件类,它调用扩展了Cordova setOnKeyListener使用人行横道时出现问题,cordova,cordova-plugins,crosswalk-runtime,crosswalk,crosswalk-project,Cordova,Cordova Plugins,Crosswalk Runtime,Crosswalk,Crosswalk Project,我正在尝试在cordova应用程序中使用crosswalk,问题是该应用程序在特定设备中为条形码阅读器使用自定义插件 我们基本上有一个插件类,它调用扩展了EditText的自定义视图,在视图中我们创建了一个onkeylister来检查是否按下了特定的键 在插件类的初始化过程中,我们使用webView.getView().setOnKeyListener(view.getScanKeyListener())创建OnKeyListener检查按键 public class ScannerInputP
EditText
的自定义视图,在视图中我们创建了一个onkeylister
来检查是否按下了特定的键
在插件类的初始化过程中,我们使用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
从未被调用,这只发生在使用人行横道时,我在调试应用程序时发现的唯一主要变化是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;
}
};
}
}
我认为这与人行横道引擎有关,但我还没有找到原因,有人对此有任何提示吗?作为更新,我确实通过流动解决了问题,我不知道XWalkView默认情况下不可聚焦,为了将来参考,该问题的解决方案是设置可聚焦(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());
}
});
}
作为一个更新,我确实通过流动解决了这个问题,我不知道XWalkView在默认情况下是不可聚焦的,为了将来参考,这个问题的解决方案是在setOnKeyListener之前设置focusable(true)、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());
}
});
}