Last active
December 9, 2015 19:39
-
-
Save vermilion1/4318799 to your computer and use it in GitHub Desktop.
Cordova Android Keyboard Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin name="KeyBoard" value="com.tamtoto.KeyBoardPlugin" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toggleKeyboard : function (show) { | |
cordova.exec(null, null, 'KeyBoard', show ? 'show' : 'hide', []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.LALALA; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import android.content.Context; | |
import android.view.KeyEvent; | |
import android.view.inputmethod.InputMethodManager; | |
import org.apache.cordova.api.CallbackContext; | |
import org.apache.cordova.api.CordovaPlugin; | |
public class KeyBoardPlugin extends CordovaPlugin { | |
public void showKeyBoard() { | |
try { | |
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); | |
cordova.getActivity().runOnUiThread(new Runnable() { | |
public void run() { | |
webView.dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MOVE_END )); | |
webView.dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MOVE_END )); | |
} | |
}); | |
} | |
catch(Exception e) {} | |
} | |
public void hideKeyBoard() { | |
InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); | |
mgr.hideSoftInputFromWindow(webView.getWindowToken(), 0); | |
} | |
@Override | |
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { | |
if (action.equals("show")) { | |
this.showKeyBoard(); | |
callbackContext.success("done"); | |
return true; | |
} | |
else if (action.equals("hide")) { | |
this.hideKeyBoard(); | |
callbackContext.success("done"); | |
return true; | |
} | |
callbackContext.error("INVALID_ACTION"); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment