2015-11-15 64 views
3

當有人點擊按鈕更改所使用的URL WebView時,我想要。在按鈕水龍頭上更改WebView URL

第一次,應用程序顯示yahoo.com。這是對的。 當有人點擊第一個或第二個按鈕時,鏈接在瀏覽器中打開。我需要做些什麼來「刷新」頁面?希望你能理解。

這裏是我的.xml文件:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button1" 
      android:id="@+id/button1" 
      android:onClick="buttonClick" 
      android:layout_weight="1.0" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button2" 
      android:id="@+id/button2" 
      android:onClick="buttonClick" 
      android:layout_weight="1.0" /> 

    </LinearLayout> 

    <WebView 
      android:id="@+id/activity_main_webview" 
      android:layout_height="0dp" 
      android:layout_width="fill_parent" 
      android:layout_weight="0.9"/> 

</LinearLayout> 

這裏是我MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    private WebView mWebView; 

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

     mWebView = (WebView) findViewById(R.id.activity_main_webview); 

     WebSettings webSettings = mWebView.getSettings(); 

     Context context = this; 
     mWebView.getSettings().setGeolocationDatabasePath(context.getFilesDir().getPath()); 

     webSettings.setJavaScriptEnabled(true); 

     mWebView.setWebViewClient(new WebViewClient()); 

     mWebView.setWebChromeClient(new WebChromeClient() { 
      public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { 
       callback.invoke(origin, true, false); 
      } 
     }); 

     mWebView.loadUrl("http://yahoo.com"); 

    } 

    public void buttonClick(View v) { 
     switch (v.getId()) { 
      case R.id.button1: 
       setContentView(R.layout.activity_main); 
       mWebView = (WebView) findViewById(R.id.activity_main_webview); 
       mWebView.loadUrl("http://google.com"); 
       break; 
      case R.id.button2: 
       setContentView(R.layout.activity_main); 
       mWebView.loadUrl("http://bing.com"); 
       break; 
     } 
    } 

} 
+0

什麼不起作用?你現在得到的結果是什麼,你想要什麼? –

+0

當有人點擊第一個或第二個按鈕時,鏈接在(外部)瀏覽器中打開。我只想在我的WebView中顯示鏈接('activity_main_webview')。 – vojvodina

+0

檢查我的答案,應該修復它。 –

回答

0

loadUrl被打開一個新的瀏覽器,因爲你重新佈局,造成了新的WebView沒有webviewclient的對象。

取出

setContentView(R.layout.activity_main); 

線,也web視圖的重新啓動。
您會得到:

public void buttonClick(View v) { 
     switch (v.getId()) { 
      case R.id.button1: 
       mWebView.loadUrl("http://google.com"); 
       break; 
      case R.id.button2: 
       mWebView.loadUrl("http://bing.com"); 
       break; 
     } 
    }