5

我有一个 PhoneGap 应用程序,它工作正常。唯一困扰我的是 PhoneGap 处理软键盘的方式。当输入模糊时它不会隐藏。它确实在 iOS 中执行此操作,但在 Android 中加载新页面时它甚至会保留。

我看看这个: http ://wiki.phonegap.com/w/page/27915465/How%20to%20show%20and%20hide%20soft%20keyboard%20in%20Android

这: https ://github.com/phonegap/phonegap-plugins/tree/master/Android/SoftKeyboard

但他们都没有为我工作,有什么想法吗?

问候, 埃里克

4

3 回答 3

7

您链接到的插件对我有用(有完全相同的问题):

$("#eingabe").blur(); //for ios
var softkeyboard = window.cordova.plugins.SoftKeyBoard;
softkeyboard.hide();

您可能使用了 Cordova 2.0.0(或更高版本)并且没有修改插件文件(为 Phonegap < 2.0 编写的)。

以下是更新的文件(我使用的文件):

软键盘.js

cordova.plugins = cordova.plugins || {};

cordova.plugins.SoftKeyBoard = {
show: function (win, fail) {
    return cordova.exec(
        function (args) { if (win !== undefined) { win(args); } },
        function (args) { if (fail !== undefined) { fail(args); } },
        'SoftKeyBoard', 'show', []
    );
},

hide: function (win, fail) {
    return cordova.exec(
        function (args) { if (win !== undefined) { win(args); } },
        function (args) { if (fail !== undefined) { fail(args); } },
        'SoftKeyBoard', 'hide', []
    );
},

isShowing: function (win, fail) {
    return cordova.exec(
        function (args) { if (win !== undefined) { win(args); } },
        function (args) { if (fail !== undefined) { fail(args); } },
        'SoftKeyBoard', 'isShowing', []
    );
}
};

软键盘.java

package org.apache.cordova.plugins;

import org.json.JSONArray;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;

public class SoftKeyBoard extends Plugin {
public SoftKeyBoard () { }

public void showKeyBoard () {
    InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT);
    ((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(webView, 0);
}

public void hideKeyBoard() {
    InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(webView.getWindowToken(), 0);
}

public boolean isKeyBoardShowing() {
    // if more than 100 pixels, its probably a keyboard...
    int heightDiff = webView.getRootView().getHeight() - webView.getHeight();
    return (100 < heightDiff);
}

public PluginResult execute(String action, JSONArray args, String callbackId) {
    if (action.equals("show")) {
        this.showKeyBoard();
        return new PluginResult(PluginResult.Status.OK, "done");
    } else if (action.equals("hide")) {
        this.hideKeyBoard();
        return new PluginResult(PluginResult.Status.OK);
    } else if (action.equals("isShowing")) {
        return new PluginResult(PluginResult.Status.OK, this.isKeyBoardShowing());
    } else {
        return new PluginResult(PluginResult.Status.INVALID_ACTION);
    }
}
}

修改config.xml

还要确保在“ res/xml/config.xml ”中添加以下行:

<plugin name="SoftKeyBoard" value="org.apache.cordova.plugins.SoftKeyBoard" />
于 2012-12-03T21:23:51.737 回答
3

这对我有用,让我的头免于过度抓挠!(也不需要插件)

var hideKeyboard = function() {
    document.activeElement.blur();
    $("input").blur();
};

解决方案在此处找到,您可以在其中找到非jQuery解决方案。

于 2015-01-12T19:30:39.283 回答
0

请注意,与键盘状态无关,Java 将调用:

return new PluginResult(PluginResult.Status.OK, this.isKeyBoardShowing());

这里的第二个参数包含真或假。所以你的javascript回调应该是:

cordova.plugins.SoftKeyBoard.isShowing(function (showing) {
    if (showing) {
        console.error("############ keyboard is open");
    } else {
        console.error("############ keyboard is closed");
    }
}
,function () {
    // never called
    console.error("############ never called!");
});

似乎 Cordova 期望成功和失败的 javascript 回调,即使在这种情况下,在两种可能的结果中都只调用成功回调。

于 2014-02-06T18:18:29.123 回答