Android开发---加载h5界面

首先 布局

只有一个webview 还有一个加载界面时候用的progressbar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layerType="hardware"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fadingEdge="none"
            android:visibility="invisible"
            android:fadingEdgeLength="0dp"
            android:scrollbars="none" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>

</LinearLayout>


java代码


public class WebViewActivity extends TitleActivity{
    private WebView webview;
    private ProgressBar progressBar;

    private ProgressBarHandler mProgressBarHandler = new ProgressBarHandler(this);
    private String mWebViewUrl;
    private String mTitle;

    public static void open(Context context,String url,String title){
        Intent intent = new Intent();
        intent.setClass(context,WebViewActivity.class);
        intent.putExtra("webUrl",url);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_web_view);
    }

    @Override
    protected void bindViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initializeViews() {
        setLeftInfo();
        if (getIntent() != null) {
            mWebViewUrl = (String) getIntent().getExtras().get("webUrl");
            mTitle = (String) getIntent().getExtras().get("title");
            setCenterTitle(mTitle);
            initViews();
        }
    }

    @Override
    protected void initializeData() {

    }

    private void initViews() {
        webview = (WebView) findViewById(R.id.webview);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        WebSettings webSettings = webview.getSettings();
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }

        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mProgressBarHandler.sendEmptyMessage(0);
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                loadUrl(view, url);
                return true;
            }
        });

        webview.setBackgroundColor(0);

        if (BussinessUtil.isValid(mWebViewUrl)){
            webview.loadUrl(mWebViewUrl);
        }
    }

    private void loadUrl(final WebView view, final String url) {
        mProgressBarHandler.sendEmptyMessage(1);
        view.loadUrl(url);
    }

    private static class ProgressBarHandler extends Handler {

        private final WeakReference<WebViewActivity> mActivity;

        public ProgressBarHandler(WebViewActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mActivity.get() == null || mActivity.get().isFinishing()) {
                return;
            }

            switch (msg.what) {
                case 0:
                    if (mActivity.get().progressBar != null && mActivity.get().webview != null) {
                        mActivity.get().progressBar.setVisibility(View.INVISIBLE);
                        mActivity.get().webview.setVisibility(View.VISIBLE);
                    }
                    break;
                case 1:
                    if (mActivity.get().progressBar != null && mActivity.get().webview != null) {
                        mActivity.get().progressBar.setVisibility(View.VISIBLE);
                        mActivity.get().webview.setVisibility(View.INVISIBLE);
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview != null && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

最后,使用的时候,只需要调用WebViewActivity中的open方法,然后将h5地址传过来就行

如:

case R.id.llAboutus:
    WebViewActivity.open(this,"http://static.isudoo.com/pages/aboutus.html","关于我们");
    break;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值