android开发步步为营之63:webview常用用法

        webview是常用的组件,特别是当前h5这么流行的情况下,本文介绍webview在项目中的常规用法。一般是webview加载的过程前会对当前网络做判断,没有网络的情况下给个提示。有网络的情况下配一个progressbar显示当前进度,好的,上代码。

        第一步:设计页面

<?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"
    android:orientation="vertical" >

    <include layout="@layout/layout_head" />
   <ProgressBar  
        android:id="@+id/pb"  
        style="?android:attr/progressBarStyleHorizontal"  
        android:layout_width="fill_parent"  
        android:layout_height="2dip"  
        android:indeterminateOnly="false"  
        android:max="100"  
        android:progressDrawable="@drawable/progress_bar_states_new" >  
    </ProgressBar>  
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


        第二步:编写activity


/**
 * 
 */
package com.android.inputmethod.latin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.inputmethod.latin.R;
import com.dotc.util.NetworkUtil;

/**
 * @author figo
 *
 */
public class CommonProblemActivity extends Activity {
    private WebView wb;
    ProgressBar pb;
    private LinearLayout mLayoutBack;
    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_problem);
       

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initView();
    }

    private void initView() {
        try {
          
            pb = (ProgressBar) findViewById(R.id.pb);  
            pb.setMax(100);  
            mLayoutBack = (LinearLayout) findViewById(R.id.layout_back);
            mLayoutBack.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    finish();
                }
            });
            mTvTitle = (TextView) findViewById(R.id.txt_title);
            mTvTitle.setText(getString(R.string.lbl_common_problem));
            //这样写将直接跳转到浏览器
//            wb.loadUrl("http://www.baidu.com");
            //使用WebViewClient web页面在当前activity中显示
            
            wb = (WebView) findViewById(R.id.webView);
            wb.getSettings().setJavaScriptEnabled(true);  
            wb.getSettings().setSupportZoom(true);    
            wb.getSettings().setBuiltInZoomControls(true);  wb.setWebViewClient(new MyWebViewClient());
            wb.setWebChromeClient(new WebViewClient() );  
            if(!NetworkUtil.isNetworkUseful(CommonProblemActivity.this))
            {
                String customHtml = "<html><body><font color='red'>current network useless!</font></body></html>";
                wb.loadData(customHtml, "text/html", "UTF-8"); 
                return;
            }
            wb.loadUrl("http://www.baidu.com");  
        } catch (Exception e) {
            if (e != null) {
                e.printStackTrace();
            }
        }

    }
    
    private class WebViewClient extends WebChromeClient {  
        @Override  
        public void onProgressChanged(WebView view, int newProgress) {  
            pb.setProgress(newProgress);  
            if(newProgress==100){  
                pb.setVisibility(View.GONE);  
            }  
            super.onProgressChanged(view, newProgress);  
        }
        
    }
}

 //如果不重载shouldOverrideUrlLoading,有些手机比如华为可能会跳转到应用外面的浏览器
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.toString().contains("chinapay.com")){//重写url方法
                view.loadUrl("http://www.chinapay.com");
                return true;
            }
            return false;
        }
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值