WebView 簡單使用一

來源:互聯網
上載者:User

標籤:

webView中首先修改添加

 

Mainfest檔案中的

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>

 

package com.imooc.android_webview;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.app.ProgressDialog;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity {    private String url = "http://www.baidu.com/";    private WebView webView;    private ProgressDialog dialog; //定義ProgressDialog    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.web);        // Uri uri = Uri.parse(url); //url為你要連結的地址        // Intent intent =new Intent(Intent.ACTION_VIEW, uri);        // startActivity(intent);        init();    }    private void init() {        // TODO Auto-generated method stub        webView = (WebView) findViewById(R.id.webView);        // WebView載入本地資源        // webView.loadUrl("file:///android_asset/example.html");        // WebView載入web資源        webView.loadUrl(url);        // 覆蓋WebView預設通過第三方或者是系統瀏覽器開啟網頁的行為,使得網頁可以在WebVIew中開啟        webView.setWebViewClient(new WebViewClient(){                        @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                // TODO Auto-generated method stub                //傳回值是true的時候控制網頁在WebView中去開啟,如果為false調用系統瀏覽器或第三方瀏覽器去開啟                view.loadUrl(url);                return true;            }            //WebViewClient協助WebView去處理一些頁面控制和請求通知                    });        //啟用支援JavaScript        WebSettings settings = webView.getSettings();        settings.setJavaScriptEnabled(true);

//WebView載入頁面優先使用緩衝載入 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub //newProgress 1-100之間的整數 if(newProgress==100) { //網頁載入完畢,關閉ProgressDialog closeDialog(); } else { //網頁正在載入,開啟ProgressDialog openDialog(newProgress); } } private void closeDialog() { // TODO Auto-generated method stub if(dialog!=null&&dialog.isShowing()) { dialog.dismiss(); //取消顯示 dialog=null; } } private void openDialog(int newProgress) { // TODO Auto-generated method stub if(dialog==null) { dialog=new ProgressDialog(MainActivity.this);//建立ProgressDialog對象 dialog.setTitle("正在載入"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定進度條顏色 dialog.setProgress(newProgress); dialog.show(); } else { dialog.setProgress(newProgress); //讓的進度條重新整理進度 } } }); } //改寫物理按鍵——返回的邏輯 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { //Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show(); if(webView.canGoBack()) { webView.goBack();//返回上一頁面 return true; } else { System.exit(0);//退出程式 } } return super.onKeyDown(keyCode, event); } }

 

用的進度條顯示載入進度。

如果從

 

WebView 簡單使用一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.