前几天在写一个项目时,要求在项目中嵌入一个WebView
本来很快就完成了,测试也没有问题。但发给新加坡时,他们测试都会出现文本框聚焦时,网页面会放大(他们用三星手机测试的)
网上查了好久参考他的方法加上去测试 http://www.cppblog.com/guojingjia2006/archive/2012/12/18/196429.html
下面我将原文copy过来
***************************************************************************************************************************
今天弄了下android webview下的几个页面。原先以为android 4+把 webview的viewport属性忽略掉了。/** * Called in response to a message from webkit telling us that 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 out of date, and // does not recognize that a textfield is in focus. In that // case, use WebView as the targeted view. // see http://b/issue?id=2457459 imm.showSoftInput(this, 0); }
wv.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub try { Field defaultScale = WebView.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(); } } } });
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 found 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 English, Thx :) PS: maybe a online framework source review website is helpful but I can't found one, if you can provide me one, it will be great. :P
try { Field defaultScale = WebView.class .getDeclaredField("mDefaultScale"); 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(); } } }
***************************************************************************************************************************
按大神的方法测试只有在三星手机找不到 mZoomManager 这个类 所以方法还是行不通
后来又找到TV端Android WebView 文本框获取焦点后自动放大的问题(代码我就只copy部分过来了)
——————————————————————————————————————————————————————————————————————
最后经过多次尝试最后找的解决办法就是:在webview.setWebChromeClient中重写onProgressChanged方法,加入view.requestFocus();就可以让文本框得到焦点后弹出键盘,同时页面也不会被放大;
针对TV端的 webview中的网页设定,可以参考一下代码
mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub super.onProgressChanged(view, newProgress); view.requestFocus(); } });
——————————————————————————————————————————————————————————————————————
我加到我的代码中,发现在手机中的确有效果,但发到新加坡,得到的回复,还是同样的问题!
到此,我表示我很伤心很伤心
想想,只能用js去处理了
正当我伤心写着demo时,我老大下班了,问我走不走。我就跟他讲了我的问题。他果断去开机去了,说试试他那边能不能改(我老大是写后台端的)
问题解决了,只加了以下两句代码---------------蛋蛋的优伤
<!-- 下面两句代码是做手机适配用的 , 加上之后手机网页就会自动适配--> <meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
哦,不好意思忘了上demo了
完美解决 Android WebView 文本框获取焦点后自动放大问题
原文地址:http://blog.csdn.net/aa1000777/article/details/45092803