android练习一之简易浏览器

//activity_main.xml
<RelativeLayout 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"
    tools:context="com.vincentlin.browser.MainActivity" >

   <FrameLayout 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       >
       
       <WebView
           android:id="@+id/webView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
       
       <ProgressBar
           android:id="@+id/progressBar"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:visibility="gone"
           />
   </FrameLayout>
</RelativeLayout>
//MainActivity.java
package com.vincentlin.browser;

import android.R.anim;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * 简易浏览器
 * @author bpm-dev
 *
 */
public class MainActivity extends Activity {
    
    WebView webView;
    ProgressBar pBar;
    
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        webView = (WebView) findViewById(R.id.webView);
        pBar = (ProgressBar) findViewById(R.id.progressBar);
        
        //设置支持Javascript
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.baidu.com");
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // 网页加载时开始调用,显示加载提示旋转进度条
                super.onPageStarted(view, url, favicon);
                pBar.setVisibility(android.view.View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // 网页加载完成时调用,隐藏加载提示旋转进度条
                super.onPageFinished(view, url);
                pBar.setVisibility(android.view.View.GONE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // 网页加载失败时调用,隐藏加载提示旋转进度条
                super.onReceivedError(view, errorCode, description, failingUrl);
                pBar.setVisibility(android.view.View.GONE);
            }
            
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //设置返回按钮返回上一个界面,并不是退出程序
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            MainActivity.this.finish();
            break;
        }
        return false;
    }

}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vincentlin.browser"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />
    <!-- 配置权限 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


你可能感兴趣的:(android,浏览器)