Android在webview中创建带有链接的Toast

时间:2016-02-29 01:33:02

标签: java android webview

如果在没有转到网址的情况下点击网页视图中的网址,如何查看吐司,只显示一个祝酒词?

wv = (WebView) findViewById(R.id.wV);
wv.loadData("<a href=\"#_ftn0\">[0]</a>", "text/html", "UTF-8");

if(OnClickedUrl == "#_ftn0")
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();

3 个答案:

答案 0 :(得分:1)

你应该覆盖网址加载,如下所示:

wv.loadData("<a href=\"http://www.example.com/#_ftn0\">[0]</a>", "text/html", "UTF-8");

wv.setWebViewClient(new MyWebViewClient());

public class MyWebViewClient extends WebViewClient {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show();
      if(url.equal(urlyouclicked)){//here you compare the url you click 
          Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
      }
      return true;
   }
}

答案 1 :(得分:0)

您可以创建shouldOverrideUrlLoading 1事件(请参阅链接1以获取API参考)并将代码放在方法中。但是,您需要指定应该覆盖加载的URL。如果要覆盖所有网址,只需删除if语句。

wv.setWebViewClient( new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //Make sure the url to override loading is the correct one
        if (url.contains("www.xxx.com")) {
            return true;
        }
        return false;
    }
    @Override
    public void onLoadResource(WebView  view, String  url){
        //Make sure the url to override loading is the correct one
        if (url.contains("www.xxx.com")) {
            //create toast message here
        }
    }
});

答案 2 :(得分:0)

除了覆盖shouldOverrideUrlLoading网址应符合以下格式:&#34; :// &#34;,否则无效。所以你可以修改&#34; #_ ftn0&#34; to&#34;例如://#_ ftn0&#34;