Android programming implements the Workround effect of hiding the keyboard when WebView executes Loadurl _android

Source: Internet
Author: User

This article illustrates the Workround effect of the Android programming implementation WebView hiding the keyboard when performing loadurl. Share to everyone for your reference, specific as follows:

WebView Workround to hide the keyboard when performing Loadurl

In writing webapp, often need to use JS call Java method, Java method execution, and then callback JS function to do something, and WebView call JS is through the Loadurl method, a loadurl will hide the soft keyboard. Because each loadurl operation will call the Clearhelpers method Clearhelpers method will call the Cleartextentry method, this method will Hidesoftkeyboard, then there is no way to hide it.

There are two ways:

1 Rewrite the Loadurl method, before calling Super.loadurl, note the size of the softkeyboard, after the execution super.loadurl the SoftKeyboard display (think all crazy ...) Not in this way)

2 In another perspective, the general invocation of Java methods will interrupt the input (because the keyboard is hidden), indicating that in this period of time should be more frequent invocation of Java methods, so this should be less time to appear, Then can give WebView a javascriptinterface, will want to pass to JS parameters in this javascriptinterface provided to JAVASCIRPT code call.

The code is as follows:

Define a class:

public class Jsinterface {public
  String getString () {
    return "2";//here is the simplest case ...
  }
}

On the WebView side:

Jsinterface = new Jsinterface ();
Webview.addjavascriptinterface (Jsinterface, "jsinterface");

To expose this javascriptinterface to JS.

On this side of the HTML JS file:

SetInterval (function () {
   counter++;
   var js = jsinterface.getstring ();
   document.getElementById ("value"). Value = document.getElementById ("value"). Value + js;
}, 1000);

Every 1 seconds through Java to fetch data, update the interface, so that SoftKeyboard will not hide

The above example is very simple, probably most people feel useless, in fact, in the Jsinterface.getstring method can do a lot of things.

When it comes to asynctask, you need to know that all asynctask are running in one thread at first, After Android1.6 to the multithreaded operation, after Android3.0, in order to avoid a number of multi-threaded problems, and changed to a single-threaded operation, if the first to Asynctask multithreaded operation, you need to build a executor, use Asynctask.executeonexecuto The R method runs. For a specific view of Http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor ( Java.util.concurrent.Executor, Params ...)

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.