gpt4 book ai didi

android - 仅当未单击任何链接时,Web View 才会检测到触摸

转载 作者:行者123 更新时间:2023-11-30 01:50:42 26 4
gpt4 key购买 nike

我在 webview 中加载静态 html 和 url。此外,我的 WebView 是可点击的,其点击 Intent 被触发。

I want to implement- when the link in url/html is clicked in webview link should load in webview. But when webview is clicked and no link is clicked intent should be fired.

我需要实现 onTouch 监听器以检测 webview 上的触摸和触发 Intent 。但是,如果我实现 ontouch,则永远不会在 webview 的 html/url 中打开内部链接

public class WebViewHotspotClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// return super.shouldOverrideUrlLoading(view, url);
// return true; does not open link
return false;//opens link in web view
}
}

hotspotWebview.setWebViewClient(new WebViewHotspotClient());
// hotspotWebview.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View view, MotionEvent motionEvent) {
// if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// startActivity(new Intent(ImageFrameActivity.this, HotspotActivity.class));
// }
// return false;
// }
// });

谢谢!

最佳答案

这里发生的事情是,当您将 ontouchlistener 分配给整个 webview 时,其他元素在它下面并且它们没有任何触摸或点击。要解决此问题,您可以执行以下操作

public class MyWebViewClicker extends Activity implements OnTouchListener,     Handler.Callback {

private static final int CLICK_WEBVIEW = 1;
private static final int CLICK_URL = 2;

private final Handler handler = new Handler(this);

private WebView webView;
private WebViewClient webviewclient;
boolean donext=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);

webView = (WebView)findViewById(R.id.myweb);
webView.setOnTouchListener(this);

webviewclient = new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
handler.sendEmptyMessage(CLICK_URL);
return false;
}
};

webView.setWebViewClient(webviewclient);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("http://www.example.com");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.myweb && event.getAction() == MotionEvent.ACTION_DOWN) {
handler.sendEmptyMessageDelayed(CLICK_WEBVIEW, 500);
}
return false;
}

@Override
public boolean handleMessage(Message msg) {
if (msg.what == CLICK_URL){
Toast.makeText(this, "URL is clicked", Toast.LENGTH_SHORT).show();
donext=true;
return true;
}
if (msg.what == CLICK_WEBVIEW){
if(donext==true)
{
//do nothing
}else{
Toast.makeText(this, "WebView is clicked", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}

关于android - 仅当未单击任何链接时,Web View 才会检测到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33094858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com