Is there a way to disable the zoom feature on input fields in webview?

后端 未结 10 1406
南笙
南笙 2021-02-05 21:18

When a user clicks in an input field or textarea, the application zooms in. Is there a simple way to disable it?

Currently have the meta tag:

meta name=\         


        
10条回答
  •  孤街浪徒
    2021-02-05 21:50

    Finally, I found a solution for my real-world project.

    - MainActivity.java
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView mWebView = (WebView) findViewById(R.id.webview);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            mWebView.setOnFocusChangeListener(new MyOnFocusChangeListener(getScale()));
        }
        Log.i(MainActivity.class.getSimpleName(), "getScale: " + getScale());
    }
    
    /**
     * Calculate the scale that we need to use manually.
     *
     * @return the scale that we need
     */
    private float getScale() {
        DisplayMetrics display = this.getResources().getDisplayMetrics();
        int width = display.widthPixels;
        Float val = Float.valueOf(width) / Float.valueOf(Constants.PAGE_WIDTH);
        return val.floatValue();
    }
    
    - MyOnFocusChangeListener.java
    
    public class MyOnFocusChangeListener implements View.OnFocusChangeListener {
    
        protected float mScale;
    
        public MyOnFocusChangeListener(float scale) {
            mScale = scale;
        }
    
        /**
         * Implement this method because we want to apply scale when focus changed.
         * @param v the View we want to apply scale when focus changed.
         * @param hasFocus has a focus or not.
         */
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                // We must try all cases because mDefaultScale will change on different versions of Android.
                try {
                    Field defaultScale = WebView.class.getDeclaredField("mDefaultScale");
                    defaultScale.setAccessible(true);
                    defaultScale.setFloat(v, mScale);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    try {
                        Field zoomManager;
                        zoomManager = WebView.class.getDeclaredField("mZoomManager");
                        zoomManager.setAccessible(true);
                        Object zoomValue = zoomManager.get(v);
                        Field defaultScale = zoomManager.getType().getDeclaredField("mDefaultScale");
                        defaultScale.setAccessible(true);
                        defaultScale.setFloat(zoomValue, mScale);
                    } catch (SecurityException e1) {
                        e1.printStackTrace();
                    } catch (IllegalArgumentException e1) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e1) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e1) {
                        try {
                            Field mProviderField = WebView.class.getDeclaredField("mProvider");
                            mProviderField.setAccessible(true);
                            Object webviewclassic = mProviderField.get(v);
                            Field zoomManager = webviewclassic.getClass().getDeclaredField("mZoomManager");
                            zoomManager.setAccessible(true);
                            Object zoomValue = zoomManager.get(webviewclassic);
                            Field defaultScale = zoomManager.getType().getDeclaredField("mDefaultScale");
                            defaultScale.setAccessible(true);
                            defaultScale.setFloat(zoomValue, mScale);
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题