android - 在 webview android 中单击图像时触发操作?

标签 android android-webview ontouchlistener

我有一个 webview 显示了一些 html 数据,其中包含一些图像和超链接一切正常,当我在 webview 中单击图像时我需要它应该启动一个 Activity 。我找到了一种检测图像的方法网页 View 使用

WebView.HitTestResult

通过使用这个我可以检测 WebView 中的图像,我把它放在 ontouchLisner 中我得到了图像的检测问题是当我滚动 WebView 时,如果我不小心将手指移过图像 Activity 将启动它是因为 ontouchLisner 是否有任何方法可以解决此问题 Activity 应该只在我单击 WebView 中的图像时触发。

我在代码中使用的 on touchlisner

    wv.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            WebView.HitTestResult hr = ((WebView) arg0).getHitTestResult();

            switch (arg1.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                break;

            case MotionEvent.ACTION_UP:


                    if (hr.getType() == 5 || hr.getType() == 8) {
                        //Detect image in webview
                        startActivity(new
                         Intent(MainActivity.this,Other.class));



                }
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                Log.d("-------", "clcik.den");

            case MotionEvent.ACTION_POINTER_UP:
                Log.d("-------", "clcik.up");
                break;

            case MotionEvent.ACTION_MOVE:

                Log.d("-------", "clcik.movee"+hr.getType());
                break;
            }

            return false;
        }
    });

最佳答案

当您在 WebView 中单击图像时,您需要打开一个上下文菜单。 为此,创建一个自定义 webview 并覆盖其 onCreateContextMenu 方法。因此,无论何时您触摸图像,它都会打开一个菜单项,您可以在该点击上实现您的逻辑。 使用此代码可能会对您有所帮助:

    public class CustomWebview extends WebView {
    public static final int ID_DO_SOMETHING = 1;


    private Context ctx;
    public CustomWebview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.ctx = context; 
    }

    public CustomWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.ctx = context;

    }

    public CustomWebview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.ctx = context;

    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);
        final HitTestResult result = getHitTestResult();
        MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                switch (item.getItemId()) {
                case ID_DO_SOMETHING:

                    // implement your logic here;

                    break;

                default:
                    break;
                }
                return true;
            }
        };

        if (result.getType() == HitTestResult.IMAGE_TYPE
                || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            // Menu options for an image.
            // set the header title to the image url
            menu.setHeaderTitle(result.getExtra());
            menu.add(0, ID_DO_SOMETHING, 0, "Your Method Name").setOnMenuItemClickListener(handler);

        }

    }

}

关于android - 在 webview android 中单击图像时触发操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037590/

相关文章:

android - ACTION_UP 不起作用

android - 调用不同类中的方法时如何获取上下文?

java - 文件部分是从 Android Webview 下载的

android - 为Android Webview启用TSL1.2

android - 在WebView android中播放youtube视频

android - 检测 AppBarLayout 和 NestedScrollView 的滚动效果

java - Android Java - 将 HttpPost 结果转换为 byte[]

android - 如何每 5 秒将 Android GPS 坐标发送到 MySQL

android - 如何设置 GoogleAccountCredential 的 setSelectedAccountName?用于在 YouTube 上上传视频

android - OnTouchListener 在 TranslationAnimation 期间不进行翻译