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

后端 未结 10 1376
南笙
南笙 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:42

    Just set in manifest:

    android:windowSoftInputMode="adjustPan"
    
    0 讨论(0)
  • 2021-02-05 21:45

    I found the answer, at least for me. It works on HTC desire and Sansung 3 low res.

    • Remove the viewport meta tags from the HTML.

    • In java apply the solution that can be found here.

      this.appView.getSettings().setSupportZoom( true ); //Modify this
      this.appView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);//Add this
      

      Remember to test if WebSettings.ZoomDensity.FAR is available to your SDK version (7 and above).

    0 讨论(0)
  • 2021-02-05 21:49

    I messed around with this alot.

    You have to have different solutions for different screen sizes and phones, done serverside.

    This works for a HTC desire for instance:

    <meta content='True' name='HandheldFriendly' />
    <meta content='width=640px; initial-scale=0.50; maximum-scale=0.50;minimum-scale=0.50;' name='viewport' />
    <meta name="viewport" content="width=640px" />
    

    This is for an iphone:

    <meta name="viewport" content="width=640px,user-scalable=0" />
    
    0 讨论(0)
  • 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();
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题