Android PhoneGap(Cordova)弹出软键盘功能


Android PhoneGap(Cordova)弹出软键盘功能,利用PhoneGap怎么去实现,纯属娱乐.

1.首先是phonegap必备的两个文件,分别是本地.java代码SoftKeyBoard.java

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

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.    
  12. /*function SoftKeyBoard() {} 
  13.   
  14.   
  15. SoftKeyBoard.prototype.show = function(win, fail) { 
  16.     return PhoneGap.exec( 
  17.             function (args) { if(win !== undefined) { win(args); } },  
  18.             function (args) { if(fail !== undefined) { fail(args); } },  
  19.             "SoftKeyBoard",  
  20.             "show",  
  21.             []);     
  22. }; 
  23.   
  24. SoftKeyBoard.prototype.hide = function(win, fail) { 
  25.     return PhoneGap.exec( 
  26.             function (args) { if(win !== undefined) { win(args); } },  
  27.             function (args) { if(fail !== undefined) { fail(args); } }, 
  28.             "SoftKeyBoard",  
  29.             "hide",  
  30.             []);     
  31. }; 
  32.   
  33. SoftKeyBoard.prototype.isShowing = function(win, fail) { 
  34.     return PhoneGap.exec( 
  35.             function (args) { if(win !== undefined) { win(args); } },  
  36.             function (args) { if(fail !== undefined) { fail(args); } }, 
  37.             "SoftKeyBoard",  
  38.             "isShowing",  
  39.             []);     
  40. }; 
  41.   
  42. PhoneGap.addConstructor(function() { 
  43.     PhoneGap.addPlugin('SoftKeyBoard', new SoftKeyBoard()); 
  44.     PluginManager.addService("SoftKeyBoard","com.zenexity.SoftKeyBoardPlugin.SoftKeyBoard"); 
  45. }); 
  46. */  
  • 1
  • 2
  • 下一页

相关内容