Android WebView的使用及调用第三方浏览器打开网页

1.编写布局
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.dejun.commonsdk.WebViewActivity">
    <ProgressBar
        android:id="@+id/commonsdk_webview_pb"
        android:layout_width="match_parent"
        android:layout_height="5dp" 
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
    <WebView
        android:id="@+id/commonsdk_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#afe"></WebView>
</LinearLayout>
2.WebView的使用

public class WebViewActivity extends BaseActivity {

    private WebView mCommonsdkWebview;
    private ProgressBar mCommonsdkWebviewPb;
    public static final String WEB_VIEW_URL="web_view_url";
    private String url;
    public static final int PB_MAX=100;//进度条的最大值
    public static void startWenViewActivity(BaseActivity fromActivity,String url){
        Bundle bundle=new Bundle();
        bundle.putString(WEB_VIEW_URL,url);
        fromActivity.openActivityWithIntent(WebViewActivity.class,bundle);
    }
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @Override
    protected void initView() {
        mCommonsdkWebview = (WebView) findViewById(R.id.commonsdk_webview);
        mCommonsdkWebviewPb = (ProgressBar) findViewById(R.id.commonsdk_webview_pb);
        mCommonsdkWebviewPb.setMax(PB_MAX);
    }
    @Override
    protected void initData(Bundle savedInstanceState) {
        url=getIntent().getStringExtra(WEB_VIEW_URL);
        mCommonsdkWebview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {//设置网页加载进度
                super.onProgressChanged(view, newProgress);
                if (newProgress>=100){
                    mCommonsdkWebviewPb.setVisibility(View.GONE);
                }else{
                    mCommonsdkWebviewPb.setProgress(newProgress);
                }

            }

        });
        mCommonsdkWebview.setWebViewClient(new WebViewClient(){
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)//android5.0以上手机才有效果
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        WebSettings settings = mCommonsdkWebview.getSettings();
        settings.setDomStorageEnabled(true);//这设置存储 不设置显示不出来
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没有网使用缓存
        settings.setJavaScriptEnabled(true);//设置网页可操作
        settings.setUseWideViewPort(true);//设置视图可缩放和下面一起设置 似乎没效果
        settings.setLoadWithOverviewMode(true);
        mCommonsdkWebview.loadUrl(url);
    }



    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_view;
    }
}

猜你喜欢

转载自blog.csdn.net/Anthonybuer/article/details/84987626