(turn) perfect solution for Android WebView text box after getting focus automatically zoom in on questions

Source: Internet
Author: User

Perfect solution for Android WebView text box to get focus after auto zoom problem

A few days ago when writing a project, a webview was required to embed in the project

It was done soon, and the test was no problem. But when they were sent to Singapore, they would have a text box focus when they tested it, and the page would zoom in (they tested it with a Samsung phone).

On the internet for a long time reference his method plus to test http://www.cppblog.com/guojingjia2006/archive/2012/12/18/196429.html

I'll copy the text here.

*************************************************************************************************************** ************

Different Android versions of WebView bottom-level implementations differ

Today I got a few pages under Android WebView. Originally thought that the Android 4+ the webview the viewport attribute to ignore.
But I got it today. Added a authorizationview.getsettings (). Setusewideviewport (True);
Several properties of the viewport function again. (test environment, several versions of 4.0+)

But there are a few problems, that is, the HTML in the time of input. When you get the focus, Android zooms back to the original mode, looking at the source code:

/** * Called in response to a message from WebKit telling us, the soft * keyboard should be launched.                */private void Displaysoftkeyboard (Boolean istextview) {Inputmethodmanager IMM = (Inputmethodmanager)        GetContext (). Getsystemservice (Context.input_method_service);  Bring it back to the default level scale so that user can enter text boolean zoom = Mzoommanager.getscale () <        Mzoommanager.getdefaultscale ();            if (zoom) {Mzoommanager.setzoomcenter (Mlasttouchx, mlasttouchy);        Mzoommanager.setzoomscale (Mzoommanager.getdefaultscale (), false);            } if (Istextview) {Rebuildwebtextview ();                if (Ineditingmode ()) {imm.showsoftinput (Mwebtextview, 0, Mwebtextview.getresultreceiver ());                if (zoom) {didupdatewebtextviewdimensions (intersects_screen);            } return; }}//used by pluginS and contenteditable.  Also used if the navigation cache is an out of date, and//does isn't recognize that a textfield are in focus.        In this//case, use WebView as the targeted view.    See http://b/issue?id=2457459 Imm.showsoftinput (this, 0); }


From the source can see, webview when to play the keyboard, will determine the current scale and default size (I tested, my own version of the default value is 1.5),
When your page viewport set initial-scale=0.5, when input gets focus, Android will zoom in to the original mode, not what we want, online check the relevant,
There is a solution:

Wv.setonfocuschangelistener (New View.onfocuschangelistener () {@Override public void Onfocuschange (View V, b Oolean hasfocus) {//TODO auto-generated Method stub try {Field Defaultscale = WEBV                Iew.class. Getdeclaredfield ("Mdefaultscale");                Defaultscale.setaccessible (TRUE);                float _s = defaultscale.getfloat (WV);                Defaultscale.setfloat (WV, scale);                float x = Wv.getscale ();            int i = 0;                } catch (Exception e) {e.printstacktrace ();                    try {Field defaultzoom = webview.class. Getdeclaredfield ("Mzoommanager");                    Defaultzoom.setaccessible (TRUE);                    Field Defaultscale = Defaultzoom.gettype (). Getdeclaredfield ("Mdefaultscale");                    Defaultscale.setaccessible (TRUE); Defaultscale.sEtfloat (Defaultzoom.get (WV), scale);                } catch (Exception ee) {ee.printstacktrace (); }            }        }    });


But the author came across another question, quoting from the exact words:

As it showed, I using reflect to find the field ' Mdefaultscale ' to control the webview.but it doesn ' t work on Android 4.1. 1 (Google Nexus), and I catch an exception--java.lang.nosuchfieldexception:mdefaultscale.then I list the fileds and fou nd the framework source seems being changed (I can only reach a field called ' Mprovider '). So how can I fix the problem (I Haven ' t got the source yet)? Thanks for reading my question with my poor 中文版, Thx:) ps:maybe A online framework source review website is helpful b UT I can ' t found one, if you can provide me one, it'll be great. :P


I tested it myself and found that the solution couldn't be solved. But leads to another problem, is not the Android version of the WebView implementation is not the same, actually see the code can be seen,
Originally WebView have mdefaultscale field, but should later moved to Mzoommanager, but I found my phone webview implementation and the author encountered the problem, only Mprovider members,
No Mzoommanager, so can only seek source code, arduous, finally found
Http://androidxref.com/4.2_r1/xref/frameworks/base/core/java/android/webkit/WebViewClassic.java,
Mprovider actually type is webviewclassic (see the source implementation), briefly mention, Webprovider is just an interface, as a member of the WebView,
Created with the factory to see the next few factory classes, and finally the Webviewclassic instance).
For Jerry Bean 4.2 This version (I am a mobile phone is my own ROM), webview implementation of another, so to get the default scaling members, as follows:

try {Field Defaultscale = webview.class. Getdeclaredfield ("mdefaultscal                      E ");                      Defaultscale.setaccessible (TRUE);                    Float SV = defaultscale.getfloat (Authorizationview);                  Defaultscale.setfloat (Authorizationview, XXX);                  } catch (SecurityException e) {e.printstacktrace ();                  } catch (IllegalArgumentException e) {e.printstacktrace ();                  } catch (Illegalaccessexception e) {e.printstacktrace ();                      } catch (Nosuchfieldexception e) {e.printstacktrace ();                           try {Field Zoommanager;                          Zoommanager = WebView.class.getDeclaredField ("Mzoommanager");                          Zoommanager.setaccessible (TRUE);   Object Zoomvalue = Zoommanager.get (Authorizationview);                       Field Defaultscale = Zoommanager.gettype (). Getdeclaredfield ("Mdefaultscale");                          Defaultscale.setaccessible (TRUE);                        Float SV = defaultscale.getfloat (zoomvalue);                      Defaultscale.setfloat (zoomvalue, XXX);                      } catch (SecurityException E1) {e1.printstacktrace ();                      } catch (IllegalArgumentException E1) {e.printstacktrace ();                      } catch (Illegalaccessexception E1) {e.printstacktrace ();                                                  } catch (Nosuchfieldexception E1) {e1.printstacktrace ();                              try {Field Mproviderfield = WebView.class.getDeclaredField ("Mprovider");                            Mproviderfield.setaccessible (TRUE);                      Mproviderfield.getclass ()      Object webviewclassic = Mproviderfield.get (Authorizationview);                               Field Zoommanager = Webviewclassic.getclass (). Getdeclaredfield ("Mzoommanager");                            Zoommanager.setaccessible (TRUE);                              Object Zoomvalue = Zoommanager.get (Webviewclassic);                              Field Defaultscale = Zoommanager.gettype (). Getdeclaredfield ("Mdefaultscale");                              Defaultscale.setaccessible (TRUE);                            Float SV = defaultscale.getfloat (zoomvalue);                          Defaultscale.setfloat (zoomvalue, XXX);                        }catch (Exception E2) {e2.printstacktrace (); }                    }                  }


Although I can get it and set it up successfully, I still can't solve it on my phone. Input gets the focus and automatically zooms in.
After thinking about, there is a realization mode can refer to: When input gets the focus, JS call Java Set default scaling rate, save the original value before setting, lose focus and reset the original value, otherwise jump to other pages, you will find that the proportion is not right. :)。

Double-click or revert back to the original as you zoomed in. So let's not dwell on this thing for the moment. This code will not work because of different Android versions if the WebView implementation is inconsistent:)

*************************************************************************************************************** ************

According to the method of the great God only in Samsung mobile phones can not find mzoommanager this class so the method is still not feasible

Later found the TV-side Android WebView text box to get focus after the automatic amplification problem (code I just copy part of it)

——————————————————————————————————————————————————————————————————————

Finally, after many attempts to find the solution is: Rewrite the onprogresschanged method in Webview.setwebchromeclient, add View.requestfocus (), you can let the text box to get the focus and then pop up the keyboard, At the same time the page will not be magnified;

For the WebView in the TV-side of the page settings, you can refer to the code

Mwebview.setwebchromeclient (New Webchromeclient () {        @Override public        void onprogresschanged (WebView view, int newprogress)        {                //TODO auto-generated method stub                super.onprogresschanged (View, newprogress);                View.requestfocus ();        } });


After overloading the Onprogresschanged method, the problem with page scaling is resolved, and the keyboard call is normal.

——————————————————————————————————————————————————————————————————————

Excerpt from: http://www.myexception.cn/web/1891062.html

I added to my code, found that in the mobile phone has a real effect, but sent to Singapore, get the reply, or the same problem!

Here I say I'm sad and sad

Think, you can only use JS to deal with

Just as I was sad to write the demo, my boss was off duty and asked me to go. I told him about my problem. He was decisive to boot up, said to try his side can change (my boss is written in the backend)

Problem solved, only add the following two lines of code---------------the superior injury of the egg



Oh, I'm sorry I forgot the demo.

Demo:webviewadapter

(turn) perfect solution for Android WebView text box after getting focus automatically zoom in on questions

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.