2014-02-15 39 views
0

我有一個WebView和編輯文本一起作爲搜索欄,但不是在字段中鍵入整個網址,我希望它使搜索也像「臉譜」和「谷歌」我在這裏找到了一個答案,但我不知道該怎麼做,有人可以幫我解決這個問題嗎?在編輯文本中顯示谷歌搜索結果 - WebView

How to show Android Google default search results in webview?

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 

public class MainActivity extends Activity { 
private WebView mWebView; 

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

    CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies 
    mWebView = (WebView) findViewById(R.id.webView1); 
    mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script 
    mWebView.setWebViewClient(new HelloWebViewClient()); 
    mWebView.loadUrl("http://www.google.com/"); //Set Home page 
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars 
    mWebView.getSettings().setDefaultFontSize(12);//Set Font Size 
    mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading 
    mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash 
    mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback  on touch 
    mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading 
    //mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 
    //mWebView.getSettings().setDisplayZoomControls(false);//Requires Api 11 
    mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb) 
    String appCachePath =  getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb) 
    mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb) 
    mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb) 
    mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb) 
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb) 


} 
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) && mWebView.canGoBack()) 

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

回答

0

因爲它在這個問題的答案是說,你需要讓你從EditText在按鈕的onClick方法得到的文本,並將其添加到URL https://www.google.com/search?q=query_string

+0

有你瞭解了佈局和視圖的基礎知識?我沒有看到任何可以輸入任何搜索查詢的EditText。 (不粗魯或任何東西。) – AndyFaizan

+0

嗯,這麼想。那麼,你需要添加一個editText和一個按鈕。在editText和button的onClick方法中輸入搜索查詢,您需要獲取搜索查詢並執行我在答案中提到的操作。如果你不知道如何處理editText和按鈕,你需要了解所有這些。這是非常基本的東西。 – AndyFaizan

+0

大聲笑。首先學習基礎知識。這包括視圖等。一旦你掌握了它,我們可以從網頁視圖繼續。 「Tab sir ke upar se nahi niklega」:P – AndyFaizan