在webview中添加Google搜索栏

时间:2014-07-14 02:46:56

标签: android webview

我有一个webview,当网址不完整时,它会自动添加“http://”,就像其他浏览器一样。但我如何添加一个单独的搜索栏,只搜索谷歌?

类似

@Override
    public void onClick(View view) {
        String url = urlEditText.getText().toString();
        String prefix = "https://www.google.com/search?q=";// Prefix


        if (validateUrl(url)) {
            webView.getSettings().setJavaScriptEnabled(true);
            url = "https://www.google.com/search?q=" + url.replace(" ", "+");//Prefix (replace spaces with a '+' sign)
            webView.loadUrl(url);

如果url字段可以用作搜索栏,那么它也很好。 (如果网址中没有Http://,Https://和域名(.something),请添加Google搜索前缀。)

      progress = (ProgressBar) findViewById(R.id.progressBar);
      progress.setMax(100);

      Button openUrl = (Button) findViewById(R.id.goButton);
      openUrl.setOnClickListener(new OnClickListener() {



          @Override
            public void onClick(View view) {
                String url = urlEditText.getText().toString();     

                if(url.endsWith(".ac") || url.endsWith(".ac.uk") || url.endsWith(".ad") || url.endsWith(".ae") || url.endsWith(".aero") || url.endsWith(".af") || url.endsWith(".ag") || url.endsWith(".ai") || url.endsWith(".al") || url.endsWith(".am") || url.endsWith(".an") || url.endsWith(".ao") || url.endsWith(".aq") || url.endsWith(".ar") || url.endsWith(".arpa") || url.endsWith(".as") || url.endsWith(".asia") || url.endsWith(".at") || url.endsWith(".au") || url.endsWith(".aw") || url.endsWith(".ax") || url.endsWith(".az") || url.endsWith(".ba") || url.endsWith(".bb") || url.endsWith(".bd") || url.endsWith(".be") || url.endsWith(".bf") || url.endsWith(".bg") || url.endsWith(".bh") || url.endsWith(".bi") || url.endsWith(".biz") || url.endsWith(".bj") || url.endsWith(".bm") || url.endsWith(".bn") || url.endsWith(".bo") || url.endsWith(".br") || url.endsWith(".bs") || url.endsWith(".bt") || url.endsWith(".bv") || url.endsWith(".bw") || url.endsWith(".by") || url.endsWith(".bz") || url.endsWith(".ca") || url.endsWith(".cat") || url.endsWith(".cc") || url.endsWith(".cd") || url.endsWith(".cf") || url.endsWith(".cg") || url.endsWith(".ch") || url.endsWith(".ci") || url.endsWith(".ck") || url.endsWith(".cl") || url.endsWith(".cm") || url.endsWith(".cn") || url.endsWith(".co") || url.endsWith(".co.uk") || url.endsWith(".com") || url.endsWith(".coop") || url.endsWith(".cr") || url.endsWith(".cs") || url.endsWith(".cu") || url.endsWith(".cv") || url.endsWith(".cx") || url.endsWith(".cy") || url.endsWith(".cz") || url.endsWith(".dd") || url.endsWith(".de") || url.endsWith(".dj") || url.endsWith(".dk") || url.endsWith(".dm") || url.endsWith(".do") || url.endsWith(".dz") || url.endsWith(".ec") || url.endsWith(".edu") || url.endsWith(".ee") || url.endsWith(".eg") || url.endsWith(".eh") || url.endsWith(".er") || url.endsWith(".es") || url.endsWith(".et") || url.endsWith(".eu") || url.endsWith(".fi") || url.endsWith(".firm") || url.endsWith(".fj") || url.endsWith(".fk") || url.endsWith(".fm") || url.endsWith(".fo") || url.endsWith(".fr") || url.endsWith(".fx.ga") || url.endsWith(".gb") || url.endsWith(".gd") || url.endsWith(".ge") || url.endsWith(".gf") || url.endsWith(".gh") || url.endsWith(".gi") || url.endsWith(".gl") || url.endsWith(".gm") || url.endsWith(".gn") || url.endsWith(".gov") || url.endsWith(".gov.uk") || url.endsWith(".gp") || url.endsWith(".gq") || url.endsWith(".gr") || url.endsWith(".gs") || url.endsWith(".gt") || url.endsWith(".gu") || url.endsWith(".gw") || url.endsWith(".gy") || url.endsWith(".hk") || url.endsWith(".hm") || url.endsWith(".hn") || url.endsWith(".hr") || url.endsWith(".ht") || url.endsWith(".hu") || url.endsWith(".id") || url.endsWith(".ie") || url.endsWith(".il") || url.endsWith(".im") || url.endsWith(".in") || url.endsWith(".info") || url.endsWith(".int") || url.endsWith(".io") || url.endsWith(".iq") || url.endsWith(".ir.is") || url.endsWith(".it") || url.endsWith(".je") || url.endsWith(".jm") || url.endsWith(".jo") || url.endsWith(".jobs") || url.endsWith(".jp") || url.endsWith(".ke") || url.endsWith(".kg") || url.endsWith(".kh") || url.endsWith(".ki") || url.endsWith(".km") || url.endsWith(".kn") || url.endsWith(".kp") || url.endsWith(".kr") || url.endsWith(".kw") || url.endsWith(".ky") || url.endsWith(".kz") || url.endsWith(".la") || url.endsWith(".lb") || url.endsWith(".lc") || url.endsWith(".li") || url.endsWith(".lk") || url.endsWith(".lr") || url.endsWith(".ls") || url.endsWith(".lt") || url.endsWith(".ltd.uk") || url.endsWith(".lu") || url.endsWith(".lv") || url.endsWith(".ly") || url.endsWith(".ma") || url.endsWith(".mc") || url.endsWith(".md") || url.endsWith(".me") || url.endsWith(".me.uk") || url.endsWith(".mg") || url.endsWith(".mh") || url.endsWith(".mil") || url.endsWith(".mk") || url.endsWith(".ml") || url.endsWith(".mm") || url.endsWith(".mn") || url.endsWith(".mo") || url.endsWith(".mobi") || url.endsWith(".mod.uk") || url.endsWith(".mp") || url.endsWith(".mq") || url.endsWith(".mr") || url.endsWith(".ms") || url.endsWith(".mt") || url.endsWith(".mu") || url.endsWith(".museum") || url.endsWith(".mv") || url.endsWith(".mw") || url.endsWith(".mx") || url.endsWith(".my") || url.endsWith(".mz.na") || url.endsWith(".name") || url.endsWith(".nato") || url.endsWith(".nc") || url.endsWith(".ne") || url.endsWith(".net") || url.endsWith(".net.uk") || url.endsWith(".nf") || url.endsWith(".ng") || url.endsWith(".nhs.uk") || url.endsWith(".ni") || url.endsWith(".nl") || url.endsWith(".no") || url.endsWith(".nom") || url.endsWith(".np") || url.endsWith(".nr") || url.endsWith(".nt") || url.endsWith(".nu") || url.endsWith(".nz") || url.endsWith(".om") || url.endsWith(".org") || url.endsWith(".org.uk") || url.endsWith(".pa") || url.endsWith(".pe") || url.endsWith(".pf") || url.endsWith(".pg") || url.endsWith(".ph") || url.endsWith(".pk") || url.endsWith(".pl") || url.endsWith(".plc.uk") || url.endsWith(".pm") || url.endsWith(".pn") || url.endsWith(".post") || url.endsWith(".pr") || url.endsWith(".pro") || url.endsWith(".ps") || url.endsWith(".pt") || url.endsWith(".pw") || url.endsWith(".py") || url.endsWith(".qa") || url.endsWith(".re") || url.endsWith(".ro") || url.endsWith(".ru") || url.endsWith(".rw") || url.endsWith(".sa") || url.endsWith(".sb") || url.endsWith(".sc") || url.endsWith(".sch.uk") || url.endsWith(".sd") || url.endsWith(".se") || url.endsWith(".sg") || url.endsWith(".sh") || url.endsWith(".si") || url.endsWith(".sj") || url.endsWith(".sk") || url.endsWith(".sl") || url.endsWith(".sm") || url.endsWith(".sn") || url.endsWith(".so") || url.endsWith(".sr") || url.endsWith(".store") || url.endsWith(".su") || url.endsWith(".sv") || url.endsWith(".sy") || url.endsWith(".sz") || url.endsWith(".tc") || url.endsWith(".td") || url.endsWith(".tel") || url.endsWith(".tf") || url.endsWith(".tg") || url.endsWith(".th") || url.endsWith(".tj") || url.endsWith(".tk") || url.endsWith(".tl") || url.endsWith(".tm") || url.endsWith(".tn") || url.endsWith(".to") || url.endsWith(".tp") || url.endsWith(".tr") || url.endsWith(".travel") || url.endsWith(".tt") || url.endsWith(".tv") || url.endsWith(".tw") || url.endsWith(".tz") || url.endsWith(".ua") || url.endsWith(".ug") || url.endsWith(".uk") || url.endsWith(".um") || url.endsWith(".us") || url.endsWith(".uy") || url.endsWith(".va") || url.endsWith(".vc") || url.endsWith(".ve") || url.endsWith(".vg") || url.endsWith(".vi") || url.endsWith(".vn") || url.endsWith(".vu") || url.endsWith(".web") || url.endsWith(".wf") || url.endsWith(".ws") || url.endsWith(".xxx") || url.endsWith(".ye") || url.endsWith(".yt") || url.endsWith(".yu") || url.endsWith(".za") || url.endsWith(".zm") || url.endsWith(".zr") || url.endsWith(".zw"))
                {
                    if(!url.startsWith("http://") && !url.startsWith("https://"))
                    {
                        url = "http://" + url;
                    }
                }
                if (validateUrl(url)) {
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.loadUrl(url);

                    MainActivity.this.progress.setProgress(0);
                }
            }





          private boolean validateUrl(String url) {
              return true;
          }
      });

    }

    private class MyWebViewClient extends WebChromeClient {
      @Override
      public void onProgressChanged(WebView view, int newProgress) {         
          MainActivity.this.setValue(newProgress);
          super.onProgressChanged(view, newProgress);
      }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
    }

    public void setValue(int progress) {
      this.progress.setProgress(progress);       
    }


    class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    {


    webview.loadUrl(url);
    return true;
    }}

    }

1 个答案:

答案 0 :(得分:0)

我相信您的问题可能是您没有看到谷歌的新搜索网址,它不再是简单或通用的网址"搜索?q ="它现在是" https://www.google.com/?gws_rd=ssl#q="只需替换这个2,我认为你应该是金色的