Step by step _ Android development course [35] _ WebView of user interface (network view) and _ androidwebview

Source: Internet
Author: User

Step by step _ Android development course [35] _ WebView of user interface (network view) and _ androidwebview

Focus on technology, enjoy life! -- QQ: 804212028.
Link: http://blog.csdn.net/y18334702058/article/details/44624305

  • Topic: WebView on the user interface (network view)
    -Similar to an embedded web browser in Android, the web page can be opened in the APP.
    Effect:

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:
Internet: webView. loadUrl ("http://www.baidu.com");
Use webView. loadUrl ("file: // android_asset/XX.html") for local files. The 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 need to add permissions to the AndroidManifest. xml file.

The instance code is as follows:

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:id="@+id/mainlayout" >    <WebView         android:id="@+id/webview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"       /></LinearLayout>

MainActivity. java:

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 {WebView webview; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); webview = new WebView (this); webview. getSettings (). setJavaScriptEnabled (true); webview. loadUrl ("https://www.baidu.com/"); // set the Web View setContentView (webview);} // override the onKeyDown (int keyCoder, KeyEvent event) of the Activity class) method public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) & webview. canGoBack () {webview. goBack (); // goBack () indicates that return true on the previous page of WebView;} return false ;}}

Do not forget to add the permission to AndroidManifest. xml:

<uses-permission android:name="android.permission.INTERNET"/>

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 need to add permissions to the AndroidManifest. xml file.

<uses-permission android:name="android.permission.INTERNET"/>

The instance code is as follows:

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:id="@+id/mainlayout" >    <WebView         android:id="@+id/webview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"       /></LinearLayout>

MainActivity. java:

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 {WebView webview; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); webview = (WebView) findViewById (R. id. webview); webview. getSettings (). setJavaScriptEnabled (true); webview. loadUrl ("https://www.baidu.com/"); webview. setWebViewClient (new Client ();} // 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 return true on the previous page of WebView;} return false;} private class Client extends WebViewClient {public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); return true ;}}}

Do not forget to add the permission to AndroidManifest. xml:

<uses-permission android:name="android.permission.INTERNET"/>

Focus on technology, enjoy life! -- QQ: 804212028.
Link: http://blog.csdn.net/y18334702058/article/details/44624305

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.