歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android PhoneGap(Cordova)彈出軟鍵盤功能

Android PhoneGap(Cordova)彈出軟鍵盤功能

日期:2017/3/1 10:16:33   编辑:Linux編程

Android PhoneGap(Cordova)彈出軟鍵盤功能,利用PhoneGap怎麼去實現,純屬娛樂.

1.首先是phonegap必備的兩個文件,分別是本地.java代碼SoftKeyBoard.java

  1. package com.tricedesigns;
  2. import org.json.JSONArray;
  3. import android.content.Context;
  4. import android.view.inputmethod.InputMethodManager;
  5. import com.phonegap.api.Plugin;
  6. import com.phonegap.api.PluginResult;
  7. public class SoftKeyBoard extends Plugin {
  8. public SoftKeyBoard() {
  9. }
  10. public void showKeyBoard() {
  11. InputMethodManager mgr = (InputMethodManager) ((Context) this.ctx).getSystemService(Context.INPUT_METHOD_SERVICE);
  12. mgr.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT);
  13. ((InputMethodManager) ((Context) this.ctx).getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(webView, 0);
  14. }
  15. public void hideKeyBoard() {
  16. InputMethodManager mgr = (InputMethodManager) ((Context) this.ctx).getSystemService(Context.INPUT_METHOD_SERVICE);
  17. mgr.hideSoftInputFromWindow(webView.getWindowToken(), 0);
  18. }
  19. public boolean isKeyBoardShowing() {
  20. int heightDiff = webView.getRootView().getHeight() - webView.getHeight();
  21. return (100 < heightDiff); // if more than 100 pixels, its probably a keyboard...
  22. }
  23. public PluginResult execute(String action, JSONArray args, String callbackId) {
  24. if (action.equals("show")) {
  25. this.showKeyBoard();
  26. return new PluginResult(PluginResult.Status.OK, "done");
  27. }
  28. else if (action.equals("hide")) {
  29. this.hideKeyBoard();
  30. return new PluginResult(PluginResult.Status.OK);
  31. }
  32. else if (action.equals("isShowing")) {
  33. return new PluginResult(PluginResult.Status.OK, this.isKeyBoardShowing());
  34. }
  35. else {
  36. return new PluginResult(PluginResult.Status.INVALID_ACTION);
  37. }
  38. }
  39. }

2.(.js文件share.js)其中的一個顯示方法

  1. var SoftKeyBoard={
  2. skbShow:function(win, fail){
  3. return cordova.exec(
  4. function (args) { if(win !== undefined) { win(args); } },
  5. function (args) { if(fail !== undefined) { fail(args); } },
  6. "SoftKeyBoard",
  7. "show",
  8. []);
  9. }
  10. }
  11. /*function SoftKeyBoard() {}
  12. SoftKeyBoard.prototype.show = function(win, fail) {
  13. return PhoneGap.exec(
  14. function (args) { if(win !== undefined) { win(args); } },
  15. function (args) { if(fail !== undefined) { fail(args); } },
  16. "SoftKeyBoard",
  17. "show",
  18. []);
  19. };
  20. SoftKeyBoard.prototype.hide = function(win, fail) {
  21. return PhoneGap.exec(
  22. function (args) { if(win !== undefined) { win(args); } },
  23. function (args) { if(fail !== undefined) { fail(args); } },
  24. "SoftKeyBoard",
  25. "hide",
  26. []);
  27. };
  28. SoftKeyBoard.prototype.isShowing = function(win, fail) {
  29. return PhoneGap.exec(
  30. function (args) { if(win !== undefined) { win(args); } },
  31. function (args) { if(fail !== undefined) { fail(args); } },
  32. "SoftKeyBoard",
  33. "isShowing",
  34. []);
  35. };
  36. PhoneGap.addConstructor(function() {
  37. PhoneGap.addPlugin('SoftKeyBoard', new SoftKeyBoard());
  38. PluginManager.addService("SoftKeyBoard","com.zenexity.SoftKeyBoardPlugin.SoftKeyBoard");
  39. });
  40. */
Copyright © Linux教程網 All Rights Reserved