我正在尝试在科尔多瓦应用程序中开始使用人行横道,问题是该应用程序使用自定义插件用于特定设备中的条形码阅读器。
我们基本上有一个插件类,它调用扩展的自定义视图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
中从未调用过,这只发生在使用人行横道时,我在调试应用程序时发现的唯一主要变化是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;
}
};
}
}
我认为这与人行横道引擎有关,但我还没有找到原因,有人对此事有任何提示吗?