1

我正在开发一个webview用于加载某些网站的应用程序。我还定制actionbar了网站导航button。这是我的布局的视图:

在我看来如何。

当我加载网站时,我的所有三个按钮都是可见的,但我希望它们只有在用户单击某个 URL 时才可见,因此用户很容易理解这些按钮的用途。

我已将所有功能添加到这些按钮。这是我的代码:

website_back = (ImageButton) cView.findViewById(R.id.website_back);
website_forward = (ImageButton) cView.findViewById(R.id.website_forward);
website_refresh = (ImageButton) cView.findViewById(R.id.website_refresh);

if (v.getId() == R.id.website_back) {
    webview.goBack();
}
if (v.getId() == R.id.website_forward) {
    webview.goForward();
}
if (v.getId() == R.id.website_refresh) {
    webview.reload();
}

我的 WebViewClient:

private class HelloWebViewClient extends WebViewClient {

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        progress.setVisibility(View.VISIBLE);
        super.onPageStarted(view, url, favicon);
    }

    public void onPageFinished(WebView view, String url) {
        progress.setVisibility(View.GONE);
        super.onPageFinished(view, url);
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        website_back.setImageResource(R.drawable.webview_back);
        view.loadUrl(url);
        return true;
    }
}

当用户在 webview 中启动的网站上单击某个 URL 时,如何仅显示这些按钮?

任何形式的帮助将不胜感激。

4

2 回答 2

4

您需要将 WebViewClient 附加到 WebView 以侦听正在获取的 URL。

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.equals(mOriginalUrl)){
            //initial url load, ignoring.
        }
        else {
            //url was navigated to - do something
        }

        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //page has finished loading
    }
});
于 2013-04-29T08:37:38.657 回答
0

this worked for me. hope it would be useful for you. try this

ExternalSiteActivity.class

public class ExternalSiteActivity extends Activity{
WebView webView;
String productURL;
ProgressBar progressBar;
ImageButton webBackButton;
ImageButton webForwButton;
ImageButton webRefreshButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.external_site_activity);
    productURL= (String) getIntent().getStringExtra("productUrl");
    System.out.println(productURL+"**********");
    progressBar=(ProgressBar)findViewById(R.id.progress);
    progressBar.setVisibility(View.INVISIBLE);
    webView = (WebView) findViewById(R.id.webview);
    webBackButton=(ImageButton)findViewById(R.id.web_back_button);
    webForwButton=(ImageButton)findViewById(R.id.web_forw_button);
    webRefreshButton=(ImageButton)findViewById(R.id.web_refresh_button);
    webView.setWebViewClient(new MyWebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(productURL);
    webBackButton.setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(webView.canGoBack())
                webView.goBack();
        }
    });
    webForwButton.setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(webView.canGoForward())
                webView.goForward();
        }
    });
    webRefreshButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            webView.reload();
        }
    });
}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        Log.v("shouldOverrideUrlLoading", "shouldOverrideUrlLoading");
        return true;
    }

    @Override
    public void onFormResubmission(WebView view, Message dontResend,
            Message resend) {
        Log.v("onFormResubmission", "onFormResubmission");
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        if(webView.canGoBack())
        {
            webBackButton.setEnabled(true);
        }
        else
        {
            webBackButton.setEnabled(false);
        }
        if(webView.canGoForward())
        {
            webForwButton.setEnabled(true);
        }
        else
        {
            webForwButton.setEnabled(false);
        }
        progressBar.setVisibility(View.VISIBLE);
        Log.v("onPageStarted", "onPageStarted");
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        Log.v("on load ","load resources----");
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

于 2013-08-24T08:25:48.090 回答