如何在WebView触发器链接中像长按一样长按链接?

时间:2014-03-05 18:20:19

标签: android android-webview

在常规浏览器中,长按链接会显示上下文菜单。我理解为什么在WebView中不会发生这种情况,而且我没有出现内容菜单。然而,我想要发生的是,长按触发链接,就好像链接被正常点击一样。目前,长按链接什么都不做。我可以做些什么来使长按链接像正常一样触发它们?

我的代码:

package com.example.testing;

import java.util.UUID;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private WebView mWebView;
    private ProgressBar mProgressBar;
    private ImageView mSplashScreen;

    @SuppressLint("SetJavaScriptEnabled") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.webview);
        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
        mSplashScreen = (ImageView) findViewById(R.id.splashscreen);

        mSplashScreen.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.VISIBLE);

        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                mProgressBar.setProgress(progress);
            }
        });

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file:///android_asset/error.html");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webview, String url) {
                mProgressBar.setProgress(0);
                mProgressBar.setVisibility(View.VISIBLE);
                webview.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mWebView.setVisibility(View.VISIBLE);
                mSplashScreen.setVisibility(View.GONE);
                mProgressBar.setVisibility(View.GONE);
            }
        });

        mWebView.loadUrl("http://example.com/");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mProgressBar.setProgress(0);
            mProgressBar.setVisibility(View.VISIBLE);
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:0)

不确定您是否可以简单地通过启用WebView为其每个链接的行为,但在此特定建议中有一个建议的替代方法。 long press within webview