WebView can load and display webpages as a browser. It uses the WebKit rendering engine to load and display webpages. There are two different ways to achieve WebView:
Step 1:
1. instantiate the WebView component in the Activity: WebView webView = new WebView (this );
2. Call the WebView loadUrl () method to set the web page to be displayed in WevView:
For Internet: webView. loadUrl ("http://www.google.com ");
Local file: webView. loadUrl ("file: // android_asset/XX.html"); local files are stored in the assets file.
3. Call the setContentView () method of Activity to display the webpage view.
4. after reading many pages through the WebView link, in order to allow WebView to support the rollback function, we need to overwrite the onKeyDown () method of the Activity class. If no processing is done, click the system rollback scissors, the entire browser calls finish () instead of rolling back to the previous page.
5. You must add permissions to the AndroidManifest. xml file. Otherwise, the Web page not available error may occur.
The following is an example:
MainActivity. java
- Package com. android. webview. activity;
-
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. view. KeyEvent;
- Import android. webkit. WebView;
-
- Public class MainActivity extends Activity {
- Private WebView webview;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- // Instantiate a WebView object
- Webview = new WebView (this );
- // Set WebView attributes to execute Javascript scripts
- Webview. getSettings (). setJavaScriptEnabled (true );
- // Load the webpage to be displayed
- Webview. loadUrl ("http://www.51cto.com /");
- // Set the Web View
- SetContentView (webview );
- }
-
- @ Override
- // Set rollback
- // Override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class
- Public boolean onKeyDown (int keyCode, KeyEvent event ){
- If (keyCode = KeyEvent. KEYCODE_BACK) & webview. canGoBack ()){
- Webview. goBack (); // goBack () indicates that the previous page of WebView is returned.
- Return true;
- }
- Return false;
- }
Add permissions to row 17 in the AndroidManifest. xml file
-
- package="com.android.webview.activity"
- android:versionCode="1"
- android:versionName="1.0">
-
-
-
- android:label="@string/app_name">
-
-
-
-
-
-
-
-
:
Step 2:
1. Declare WebView in the layout File
2. instantiate WebView in Activity
3. Call the WebView loadUrl () method to set the web page to be displayed in WevView.
4. To enable WebView to respond to the hyperlink function, call the setWebViewClient () method to set the WebView
5. After reading many pages through the WebView link, in order to allow WebView to support the rollback function, we need to overwrite the onKeyDown () method of the Activity class. If no processing is performed, click the system rollback scissors, the entire browser calls finish () instead of rolling back to the previous page.
6. You must add permissions to the AndroidManifest. xml file. Otherwise, the Web page not available error occurs.
The following is an example:
MainActivity. java
- Package com. android. webview. activity;
-
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. view. KeyEvent;
- Import android. webkit. WebView;
- Import android. webkit. WebViewClient;
-
- Public class MainActivity extends Activity {
- Private WebView webview;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- Webview = (WebView) findViewById (R. id. webview );
- // Set WebView attributes to execute Javascript scripts
- Webview. getSettings (). setJavaScriptEnabled (true );
- // Load the webpage to be displayed
- Webview. loadUrl ("http://www.51cto.com /");
- // Set the Web View
- Webview. setWebViewClient (new HelloWebViewClient ());
- }
-
- @ Override
- // Set rollback
- // Override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class
- Public boolean onKeyDown (int keyCode, KeyEvent event ){
- If (keyCode = KeyEvent. KEYCODE_BACK) & webview. canGoBack ()){
- Webview. goBack (); // goBack () indicates that the previous page of WebView is returned.
- Return true;
- }
- Return false;
- }
-
- // Web View
- Private class HelloWebViewClient extends WebViewClient {
- @ Override
- Public boolean shouldOverrideUrlLoading (WebView view, String url ){
- View. loadUrl (url );
- Return true;
- }
- }
- }
Main. xml
-
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- android:id="@+id/webview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
-
Add permissions to row 17 in the AndroidManifest. xml file
-
- package="com.android.webview.activity"
- android:versionCode="1"
- android:versionName="1.0">
-
-
-
- android:label="@string/app_name">
-
-
-
-
-
-
-
-
:
Reference Article address: http://www.zingson.com/blog/p145.html?
This article fixed address: http://www.zingson.com/blog/p145.html? Fixed address: http://www.zingson.com/blog/p145.html