How to Add Prefix in WebView URL?

2020-01-20 09:51发布

I have a URL Field in my WebView, it works fine but i want it to add a prefix ("https://www.google.com/search?q=") in the url every time i press the search button

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;

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

urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebViewClient());


CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
webView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
webView.setWebViewClient(new HelloWebViewClient());
webView.loadUrl("http://www.google.com/"); //Set Home page
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
webView.getSettings().setDefaultFontSize(12);//Set Font Size
webView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
webView.getSettings().setPluginState(PluginState.ON);//Enable Flash
webView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
  //webView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 

webView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
String appCachePath =         getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
webView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
webView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
webView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)


 //////888888888888888888888888888888888888888888888888888888888888888888888   

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 (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);       

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


webview.loadUrl(url);
return true;
}
}
@Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
{

 if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())

{
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

3条回答
SAY GOODBYE
2楼-- · 2020-01-20 10:12

In the onClick() method of the button, just concatenate url and the query.

    @Override
    public void onClick(View view) {
        String url = urlEditText.getText().toString();
        String prefix = "https://www.google.com/search?q=";
        if(!url.startsWith("http://") && !url.startsWith("https://"))
        {
            url = prefix + url;
        }
        if(url.endsWith(".com") || url.endsWith(".as") || url.endsWith(".uk") || url.endsWith(".biz"))
        {
            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);
        }
    }
查看更多
欢心
3楼-- · 2020-01-20 10:29

Nothing could be easier:

Before

webview.loadUrl(url);

Add

url = "https://www.google.com/search?q=" + url;

Just keep in mind that the spaces must be replaced by a plus sign (+).

So, the url string will become:

url = "https://www.google.com/search?q=" + url.replace(" ", "+");
查看更多
混吃等死
4楼-- · 2020-01-20 10:29

Here is how I do it

String q = ((EditText)bar.findViewById(R.id.browser_searchbar)).getText().toString();
    if (q.contains(".")){
        if (q.startsWith("http://")||q.startsWith("https://"))
            WV.loadUrl(q);
        else if (q.startsWith("www."))
            WV.loadUrl("http://"+q);
        else
            WV.loadUrl("http://"+q);
    }
    else if (q.startsWith("about:")||q.startsWith("file:"))
        WV.loadUrl(q);
    else
        WV.loadUrl("http://www.google.com/#q="+q);
查看更多
登录 后发表回答