webview

package com.example.administrator.webview;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
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.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity implements Button.OnClickListener{
    private Button btn_open_system_browser;
    private Button btn_webview;

    private EditText editText;
    private WebView webview;

    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_open_system_browser = (Button)findViewById(R.id.btn_open_system_browser);
        btn_webview = (Button)findViewById(R.id.btn_webview);
        editText = (EditText)findViewById(R.id.editText);
        webview = (WebView)findViewById(R.id.webview);

        btn_open_system_browser.setOnClickListener(this);
        btn_webview.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Uri uri = Uri.parse(editText.getText().toString());
        switch (v.getId()){
            case R.id.btn_open_system_browser:
                /**
                 * 打开系统浏览器
                 */
                Log.e("main","sys");
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
                break;
            case R.id.btn_webview:
                Log.e("main", "webview");
                //需要 访问网络的权限
                webview.loadUrl(uri.toString()); //还是会在系统浏览器中打开

                //网页在webview中打开
                webview.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true; //返回true 在webview中打开 返回false 在第三方中打开
                    }
                });

                //启用javascript
                WebSettings settings = webview.getSettings();
                settings.setJavaScriptEnabled(true);

                // 启用缓存
                settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

                //网页加载 loading 进度条
                webview.setWebChromeClient(new WebChromeClient(){
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        if(newProgress == 100){
                            closeDialog();
                        }else{
                            openDialog(newProgress);
                        }
                        super.onProgressChanged(view, newProgress);
                    }

                    private void closeDialog(){
                        if(progressDialog != null && progressDialog.isShowing()){
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                    }
                    private void openDialog(int newProgress){
                        if(progressDialog == null){
                            progressDialog = new ProgressDialog(MainActivity.this);
                            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                            progressDialog.setTitle("网页加载中");
                            progressDialog.setMessage("网页加载中,请稍后。。。");
                            progressDialog.setIcon(R.mipmap.ic_launcher);
                            progressDialog.setProgress(newProgress);
                            progressDialog.show();
                        }else{
                            progressDialog.setProgress(newProgress);
                        }
                    }
                });
                break;
        }
    }


    //返回键 重写 webview后退
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(webview.canGoBack()){
                Log.e("goBack", "yes");
                webview.goBack();
                return true;
            }else{
                Log.e("goBack","no");
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值