【发布时间】:2016-08-15 15:43:38
【问题描述】:
我有以下问题,我需要完全禁用本机键盘。键盘应该只在我调用 show() 时显示,并在我调用 close() 函数时隐藏(这将在一个按钮上供用户切换键盘)。 需要完全禁用在单击字段和焦点时显示的键盘。
在 Stackoverflow 上我发现了这个: InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(editText.getWindowToken(), 0);
所以我的想法是在“Init”中(IonicKeyboard.java 中的第 52 行)我需要更改它。
if ("init".equals(action)) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
//new Logic on init
View v = cordova.getActivity().getCurrentFocus();
((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
DisplayMetrics dm = new DisplayMetrics();
cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
final float density = dm.density;
//http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
int previousHeightDiff = 0;
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
PluginResult result;
int heightDiff = rootView.getRootView().getHeight() - r.bottom;
int pixelHeightDiff = (int)(heightDiff / density);
if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
String msg = "S" + Integer.toString(pixelHeightDiff);
result = new PluginResult(PluginResult.Status.OK, msg);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
String msg = "H";
result = new PluginResult(PluginResult.Status.OK, msg);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
previousHeightDiff = pixelHeightDiff;
}
};
rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
dataResult.setKeepCallback(true);
callbackContext.sendPluginResult(dataResult);
}
});
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
遗憾的是,这根本不起作用......
我想我也必须更改关闭/显示功能,但我不确定正确的代码是什么,我不知道此更改是否会影响其他键盘行为。 (但我基本上需要没有键盘的焦点)
我也发现了这个Cordova Plugin
这看起来很有希望,但我决定在 Ionic Keyboard Plugin 中更改它,因为我也需要在 Windows 中实现相同的行为。 很高兴有人能在这里帮助我。
问候克里斯托弗
【问题讨论】:
-
使用
ngReadonly使元素只读,这样它们就不能被聚焦,因此不会触发键盘?然后在您手动调用键盘显示时启用焦点.. -
主要问题是我需要在现场集中注意力,因为我通过扫描仪获得外部输入。所以我需要专注于现场,但只有当用户想要通过按钮点击显示或隐藏时才需要键盘
-
@stackg91 你看过这个链接吗 - stackoverflow.com/questions/10611833/…
-
我几乎尝试了那里描述的所有内容,从更改 Manifest 中的 SoftInput,到在 Ionic Keyboard 插件的 init 函数中调用 InputMethodManager,但似乎没有任何效果,我什至不确定更改是否我什至生效了,因为行为根本没有改变,但我只知道可以改变行为的清单和键盘插件
-
我尝试了几乎所有来自 stateHidden, stateAlwaysHidden, 0, Hide_implicit 的可能性
标签: javascript android cordova ionic-framework keyboard