首页 文章

Cordova Ionic Keyboard插件在“Init”上完全禁用

提问于
浏览
5

我有以下问题,我需要完全禁用本机键盘 . 键盘只应在我调用show()时显示,并在我调用close()函数时隐藏(这将在用户切换键盘的Button上) . 单击Field和on Focus时显示的键盘需要完全禁用 .

在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中也需要相同的行为 . 很高兴,如果有人能帮助我在这里 .

关心克里斯托弗

1 回答

  • 0

    这可以通过使用 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();
    }
    

相关问题