[Android entry to project combat -- 8.1] - WebView usage

        How to use the HTTP protocol on the mobile phone to interact with the server and analyze the data returned by the server.

1. WebView

        When we need to display other people's web pages in the application, how to achieve it easily? Definitely not writing a web page yourself, Android provides a WebView control, with which you can embed a web page in your own application.

The role of WebView

  • Display and render web pages
  • Directly use html files (on the web or in local assets ) for layout
  • Can be called interactively with JavaScript

Disadvantages of WebView

You can refer to this article: https://www.cnblogs.com/lee0oo0/p/4026774.html

Use of WebView

So how to use WebView? The following is realized through an example of embedding Baidu.

Create a new WebViewTest project,

Modify the activity_main.xml code as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
</LinearLayout>

Modify the MainActivity code as follows:

        The getSettings() method can set some browser properties; the setJavaScriptEnabled() method allows WebView to support JavaScript scripts; the setWebViewClient() method is used to hope that the target webpage is still displayed in the current WebView when it is necessary to jump from one webpage to another , instead of opening the system browser; the loadUrl() method passes in the URL, and the corresponding web page content can be displayed.

public class MainActivity extends AppCompatActivity {

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

        WebView webView = (WebView) findViewById(R.id.web_view);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.baidu.com");
    }
}

Finally, you need to declare network permissions and modify the AndroidManifest.xml file, as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.webviewtest">
    
    <uses-permission android:name="android.permission.INTERNET"/>

............

The effect is as follows:

2. Other uses of WebView

 

 

 

 

Guess you like

Origin blog.csdn.net/Tir_zhang/article/details/130453991