Android development: WebView of controls
How can I open a Web site in an Android app? Google provides us with a solution. Now let's take a look at the WebView control.
To facilitate the summary, we will summarize the following results:
First, let's take a look at its layout file. The entire interface is divided into two parts: the upper part is a title bar effect, which consists of two buttons and a TextView, the lower part is a WebView control through AndroidManifest. xml removes the system title (if you do not understand it, please refer to my previous blog: common Android attributes. For your convenience, the following code is provided:
- <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Xmlns: tools = "http://schemas.android.com/tools"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: orientation = "vertical"
- Tools: context = ". MainActivity">
-
- <LinearLayout
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: weightSum = "1">
- <Button
- Android: id = "@ + id/quit"
- Android: layout_gravity = "left"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "back"/>
- <TextView
- Android: id = "@ + id/web"
- Android: layout_gravity = "center"
- Android: gravity = "center"
- Android: layout_width = "222dp"
- Android: layout_height = "wrap_content"
- Android: layout_weight = "1.13"/>
- <Button
- Android: id = "@ + id/news"
- Android: layout_gravity = "right"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "refresh"/>
- </LinearLayout>
- <WebView
- Android: id = "@ + id/webView"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"/>
-
- </LinearLayout>
Finally, we started to write MainActivity. java:
- Public class MainActivity extends Activity {
- Private TextView mTextView;
- Private WebView mWebView;
- Private Button mbreak;
- Private Button mnews;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- Init ();
- }
- Public void init (){
- MTextView = (TextView) findViewById (R. id. web );
- MWebView = (WebView) findViewById (R. id. webView );
- Mbreak = (Button) findViewById (R. id. quit );
- Mnews = (Button) findViewById (R. id. news );
- Mbreak. setOnClickListener (new myListener ());
- Mnews. setOnClickListener (new myListener ());
- MWebView. loadUrl ("http://www.baidu.com/"); // sets the URL to open
-
- MWebView. setWebChromeClient (new WebChromeClient (){
- @ Override
- Public void onReceivedTitle (WebView view, String title ){
- Super. onReceivedTitle (view, title );
- MTextView. setText (title); // display the opened URL Information
- }
- });
-
- MWebView. setWebViewClient (new WebViewClient (){
- @ Override
- Public boolean shouldOverrideUrlLoading (WebView view, String url ){
- View. loadUrl (url );
- Return super. shouldOverrideUrlLoading (view, url );
- }
- });
- }
-
- // Click event monitoring
- Class myListener implements View. OnClickListener {
- @ Override
- Public void onClick (View view ){
- Switch (view. getId ()){
- Case R. id. quit:
- Finish ();
- Break;
- Case R. id. news:
- MWebView. reload ();
- Break;
- }
- }
- }
Do not forget to add the network declaration in AndroidManifest. xml: <uses-permission android: name = "android. permission. INTERNET"/>
This is the end of our WebView introduction.