Listen Webview Key Events from software keyboard in Android Activity -
Listen Webview Key Events from software keyboard in Android Activity -
is possible handle software keyboard events webview in host android application?
for example, can application's activity hear typed content in search field of webview displaying google website?
considering method described below possible if overwrite returning true, unfortunatelly not able it. ideas?
public boolean shouldoverridekeyevent (webview view, keyevent event) added in api level 1 give host application chance handle key event synchronously. e.g. menu shortcut key events need filtered way. if homecoming true, webview not handle key event. if homecoming false, webview handle key event, none of super in view chain see key event. default behavior returns false. parameters view webview initiating callback. event key event. returns true if host application wants handle key event itself, otherwise homecoming false
i think shouldoverridekeyevent
method tells scheme "window" should getting notified of key events; not pass webview javascript events activity.
if need pass info webview activity, can utilize javascriptinterface. note possible on website control; otherwise security loophole, providing app access sensitive data.
first, create class deed interface:
class myjavascriptinterface{ //methods phone call via js in webview go here }
then initialize interface on webview:
mwebview.getsettings().setjavascriptenabled(true); mwebview.addjavascriptinterface(new myjavascriptinterface(), "jsinterface");
now, said wanted pass typed content text field activity. that, register event listener text field on blur, fire when field loses focus.
<!-- html element hear blur --> <input id="field" type="text" /> <script> //communicate javascript interface var jsfieldblur = function(field){ //only phone call if interface exists if(window.jsinterface){ window.jsinterface.onfieldblur(field.id, field.value); } }; //obtain reference dom element var field = document.getelementbyid("field"); //attach blur listener field.addeventlistener("blur", function( event ) { jsfieldblur(event.target); }, true); </script>
finally, need add together onfieldblur
method myjavascriptinterface class can called via javascript. methods defined way must preceded @javascriptinterface
in order visible webview.
class myjavascriptinterface{ @javascriptinterface public void onfieldblur(string fieldid, string fieldvalue){ //do value toast.maketext(getcontext(), fieldid+"="+fieldvalue, toast.length_long).show(); } }
android webview keyevent
Comments
Post a Comment