隐藏模糊,在phonegap中的软键盘

时间:2012-05-08 14:14:09

标签: android cordova hide android-softkeyboard

我有一个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

但他们都不适合我,任何想法?

此致 埃里克

3 个答案:

答案 0 :(得分:7)

您链接的插件为我工作(具有完全相同的问题):

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

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

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

softkeyboard.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', []
    );
}
};

SoftKeyBoard.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" />

答案 1 :(得分:3)

这对我有用,并且免于过度刮伤! (也不需要插件)

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

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

答案 2 :(得分:0)

请注意,独立于键盘的状态,Java将调用:

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

此处的第二个参数包含true或false。所以你的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回调,即使在这种情况下只会在两种可能的结果中调用成功回调。