Listen to key events of Webview from the software keyboard in Android activity

Can I handle soft keyboard events from a web browser in an Android host app?

For example, can my Activity application listen to the typed text in the search box of a website displaying a Google website?

Given the method described below, this would be possible if I rewrote it, returning true, but, unfortunately, I could not do this. Any ideas?

public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event)

Added in API level 1
Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key     events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.

Parameters
view    The WebView that is initiating the callback.
event   The key event.
Returns
True if the host application wants to handle the key event itself, otherwise return false
+4
source share
2 answers

, shouldOverrideKeyEvent , "" ; , JavaScript WebView Activity.

WebView , JavascriptInterface. , -, ; , .

, :

class MyJavaScriptInterface{
    //Methods to call via js in the WebView go here
}

WebView:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "JSInterface");

, Activity. , , .

<!-- HTML element to listen for blur -->
<input id="field" type="text" />

<script>
  //Communicate with Javascript Interface
  var jsFieldBlur = function(field){
      //Only call if the interface exists
      if(window.JSInterface){
          window.JSInterface.onFieldBlur(field.id, field.value);
      }
  };

  //Obtain reference to DOM element
  var field = document.getElementById("field");

  //Attach blur listener
  field.addEventListener("blur", function( event ) {
      jsFieldBlur(event.target);   
  }, true);
</script>

, onFieldBlur MyJavascriptInterface, javascript. , , @JavascriptInterface, WebView.

class MyJavaScriptInterface{
    @JavascriptInterface
    public void onFieldBlur(String fieldId, String fieldValue){
        //Do something with value
        Toast.makeText(getContext(), fieldId+"="+fieldValue, Toast.LENGTH_LONG).show();
    }
}
+1
public class WebViewDemo extends Activity {

WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.loadUrl("file:///android_asset/test.html");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

   // myWebView.setWebViewClient(new WebViewClient());
    myWebView.setWebViewClient(new MyWebViewClient());
}

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
    }
}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.google.com")) {
            // This is my web site, so do not override; let my WebView load the page
            Toast.makeText(getApplicationContext(), "www.google.com", Toast.LENGTH_SHORT).show();
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the BACK key and if there history
    if ((keyCode ==  KeyEvent.KEYCODE_ENTER)) {

       // here you can got key event of enter key ( whether you used as for search of any other option

        return true;
    }
    // If it wasn't the BACK key or there no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

0

All Articles