【问题标题】:gestureScanner functions overrides hyperlinks click手势扫描仪功能覆盖超链接点击
【发布时间】:2017-10-04 11:51:42
【问题描述】:

我对 web 视图中的超链接有疑问。我浏览了所有解决类似问题的链接,但没有找到任何解决相同问题的链接。

我有一个自定义 Web 视图,它对特定手势执行一些操作。我想这是禁用超链接的触摸。

如何保持手势并启用超链接并调用 shouldOverrideUrlLoading?

这是我拥有的自定义网页视图:

public class newBTWebView extends WebView implements QuickAction.OnDismissListener, PopoverView.PopoverViewDelegate, GestureDetector.OnGestureListener {
public newBTWebView(Context context) {
    super(context);
    this.ctx = context;
    setup(context);
    init(context);

}

public newBTWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.ctx = context;
    setup(context);
    init(context);

}

public void init(Context context) {
    this.context = context;

    setInitialScale(100);
    this.getSettings().setJavaScriptEnabled(true);

    Configuration conf = getResources().getConfiguration();

    int screenLayout = 1;
    try
    {
        Field field = conf.getClass().getDeclaredField("screenLayout");
        screenLayout = field.getInt(conf);
    }
    catch (Exception e) {

    }

    loadingNewChap = false;

    screenType = screenLayout & 15;

    gestureScanner = new GestureDetector(this);
    this.setVerticalScrollBarEnabled(false);
    this.setHorizontalScrollBarEnabled(false);


    this.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();

            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                countDown++;
            }

            if(event.getAction() == MotionEvent.ACTION_UP  && isLongPress)
            {
                 //do selection

            }
            return gestureScanner.onTouchEvent(event);
        }
    });
}

我还实现了手势的功能:onFlingDo、onSingleTapUp、onShowPress、onLongPress、onDown

这就是我在活动中调用 webview 的方式:

testWV = (newBTWebView) findViewById(R.id.mywebview1);
testWV.setVerticalScrollBarEnabled(false);
testWV.setHorizontalScrollBarEnabled(false);

testWV.setWebViewClient(new WebViewClient() {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)
     {
           super.shouldOverrideUrlLoading(view, url);
           return false;
     }

     @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
     }

     @Override
     public void onPageFinished(WebView view, String url) {
     }
}

我怎样才能保持手势并仍然捕获超链接点击?

【问题讨论】:

  • 亲爱的投票者,请在投票中说明其背后的原因,以便我修复它。谢谢。
  • 既然你问:反对票可能是因为不必要的编辑战。应始终保留好的编辑,除非它们改变了帖子的含义。

标签: android webview hyperlink gesture


【解决方案1】:
this.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();

        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            countDown++;
        }

        if(event.getAction() == MotionEvent.ACTION_UP  && isLongPress)
        {
             //do selection

        }
        gestureScanner.onTouchEvent(event);
        return false;
    }
});

在 onTouch 回调中,返回 false 以实现默认触摸行为。

【讨论】:

  • 我会尝试以下并回复您,谢谢您的贡献
【解决方案2】:

请使用下面的代码它将解决问题:

testWV .setWebChromeClient(new WebChromeClient()); 
testWV .getSettings().setJavaScriptEnabled(true); 
testWV .getSettings().setPluginsEnabled(true);
testWV .loadUrl(url); 
testWV .setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
  view.loadUrl(url);
  return true;
}
});

【讨论】:

  • 感谢您的回答,请注意超链接甚至没有点击调用此函数 shouldOverrideUrlLoading
猜你喜欢
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多