0

我有一个通用活动,其中包含一个 webview 来显示不同的基于 html 的内容。

每个 html 内容都有这样的元标记。

<meta name="viewport" content="width=device-width,height=device-height, minimum-scale=0.4, maximum-scale=3,initial-scale=0.5"> 

并且 WebView 以编程方式配置为使用这些代码适应屏幕中的内容。

WebView webView = (WebView) findViewById(R.id.webview1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);        
        webView.getSettings().setUseWideViewPort(true);     
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);    
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setLightTouchEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);

        webView.setBackgroundColor(0);

不幸的是,元代码和 webview 配置都不适合屏幕中的内容;但双击适合内容非常好。所以我想知道我可以以编程方式模拟对 webview 的双击吗?

4

1 回答 1

2
/**
 * 
 */
package com.y2cf.hoppr.utils.viewutils;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.webkit.WebView;

/**
 * @author rajesh
 *
 */
public class MyWebView extends WebView {


    private GestureDetector gestureDetector;

    /**
     * @param context
     */
    public MyWebView(Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, new GestureListener());
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     */
    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MyWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        gestureDetector = new GestureDetector(context, new GestureListener());
        // TODO Auto-generated constructor stub
    }


    public void simulateDoubleTap()
    {

        simulateDoubleTapEvent(0);
        simulateDoubleTapEvent(2);
        simulateDoubleTapEvent(2);
        simulateDoubleTapEvent(1);
        simulateDoubleTapEvent(0);
    }

    private void simulateDoubleTapEvent(int action)
    {
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        float x = 0.0f;
        float y = 0.0f;
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        int metaState = 0;
        MotionEvent me = MotionEvent.obtain(
            downTime, 
            eventTime, 
            action, 
            x, 
            y, 
            metaState
        );
        dispatchTouchEvent(me);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();

            Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");

            return true;
        }
    }

}

创建一个这样的类并调用simulateDoubleTap() 方法。希望这会有所帮助,并且您的 webview 应该是此类的一个实例。

于 2012-04-12T13:06:30.803 回答