【问题标题】:Cordova Ionic Keyboard plugin disable completely on “Init”Cordova 离子键盘插件在“初始化”上完全禁用
【发布时间】: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


【解决方案1】:

这可以通过使用ng-disabled 禁用所有输入并稍后使用Ionic Keyboard Plugin 显示键盘来实现。

如果您不希望输入在禁用时显示不同,您可以使用 :disabled 选择器使用 CSS 覆盖此样式。

加载时隐藏:

<input ng-disabled="hideKeyboard">
hideKeyboard = true;

显示函数:

<input ng-disabled="hideKeyboard">
function showKeyboard(){
  hideKeyboard = false;
  cordova.plugins.Keyboard.show();
}

【讨论】:

  • 嗯,但是当我禁用该字段时,我仍然需要没有键盘的焦点,我不会获得焦点吗?
  • 没错,但如果你没有键盘,为什么需要专注?粘贴文本?
  • 从物理扫描仪扫描条形码,有些设备带有物理键盘,我不需要看到软键盘
  • 您可以使用函数更改禁用输入的值,例如在条形码扫描仪的回调中。我知道如果你有一个物理键盘,这可能是一个问题,因为我是耶,你需要专注于一个项目。
  • 您确定在连接物理键盘时没有允许隐藏键盘的本机按钮吗?我似乎记得上次使用蓝牙键盘时能够显示或隐藏软键。
猜你喜欢
  • 2016-02-27
  • 1970-01-01
  • 2014-10-20
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多