Android programming implements the workround effect of hiding the keyboard when webview executes loadUrl

  • 2020-09-16 07:47:42
  • OfStack

This article illustrates the workround effect of Android programming to hide the keyboard when webview executes loadUrl. To share for your reference, the details are as follows:

webview Hides the workround of the keyboard when performing loadUrl

When writing webapp, it is often necessary to call the Java method with JS. When the java method is finished, the JS function is called back to do 1 thing. When webview calls js with loadUrl method, 1 will execute loadUrl and hide the soft keyboard. Since every loadUrl operation calls the clearHelpers method, the clearHelpers method calls the clearTextEntry method, and the hideSoftKeyboard method calls the clearTextEntry method, is there any way not to hide it?

There are two ways:

1) Override the loadUrl method to write down the size of ES31en.loadUrl before calling super.loadUrl and display softkeyboard after super.loadUrl (crazy to think... Not in this way.)

2) consider from a different Angle, 1 call Java method will interrupt input (because hidden keyboard), in this period of time should be 1 more frequently call Java method, so this kind of circumstance should be less appeared, so I can give webview1 javascriptinterface, pass want to js parameters in this javascriptinterface offer javascirpt code calls.

The code is as follows:

Define 1 class:


public class JSInterface {
  public String getString(){
    return "2" ; // This is where the argument is returned, and this is the simplest case. 
  }
}

At webview:


jsInterface = new JSInterface();
webView.addJavascriptInterface( jsInterface, "JSInterface" );

Expose this javascriptinterface to js

On the html js file:


setInterval(function() {
   counter++;
   var js = JSInterface.getString();
   document.getElementById( "value").value = document.getElementById("value" ).value + js;
}, 1000);

Every 1 second fetch the data from Java and update the interface so that softkeyboard will not be hidden

The above example is so simple that most people may find it useless. In fact, there are many things that can be done in the JSInterface.getString method.

Speaking of AsyncTask, we need to know that at the beginning all AsyncTask were run in one thread successively, after Android1.6, it was changed to multi-thread running; after Android3.0, in order to avoid the problem of one multi-thread, it was changed to single-thread running again. If you want AsyncTask multi-thread running first, you need to build one Executor, and use AsyncTask.executeOnExecutor method to run. Specific view http: / / developer. android. com/reference/android/os/AsyncTask html # executeOnExecutor (java. util. concurrent. Executor, Params...).

I hope this article has been helpful in Android programming.


Related articles: