2016-02-05 43 views
0

在這裏,我正在開發一個Android應用以及網頁。我需要在我的android活動中顯示網頁內容而不使用瀏覽器。如何在不使用瀏覽器的情況下在我們的Android應用中顯示網頁內容

我對此需求使用WebView。但問題是如果我點擊它在瀏覽器中打開的按鈕。

在這裏,我必須在我的Android應用程序中顯示該頁面作爲新的活動。

這是我爲這個問題試過的代碼。

webview.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 
    tools:ignore="MergeRootFrame"> 

    <WebView 
     android:id="@+id/activity_tab_host_webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</FrameLayout> 

這是我的Admissionactivity這需要顯示的網頁內容

public class AdmissionActivity extends Activity { 

     private WebView webView; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.web_view); 

      webView = (WebView) findViewById(R.id.activity_tab_host_webview); 
      webView.loadUrl("http://www.google.com"); 
} 
} 

這是該按鈕的代碼單擊

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.about_college_listview); 
     intro = (TextView) findViewById(R.id.intro_button); 
     intro.setText("Introduction"); 
     intro.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent admAct = new Intent(getApplicationContext(), AdmissionActivity.class); 
       // Clears History of Activity 
       admAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(admAct); 


      } 

     }); 
    } 

回答

0

只實現web客戶端並在loadUrl之前設置它。最簡單的方法是:

myWebView.setWebViewClient(new WebViewClient()); 
+0

感謝您的答覆,我將檢查,並告知你 –

+0

當然這個人,我等待着你的迴應。 –

+0

感謝它運作良好 –

0
//prevent web browser launch with loading http url 
    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      return false; 
     } 
    }); 
相關問題