WebView相关

WebView相关


WebView的后退

方式一

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

方式二

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

使用intent跳转到指定网页

Uri uri = Uri.parse("http://www.sohu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

webview浏览器url输入栏

btn_search.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        String url = et_text.getText().toString();

        if (url == null || "".equals(url)) {
            Toast.makeText(getApplicationContext(), "请输入", Toast.LENGTH_SHORT).show();
        } else {
            if (!url.startsWith("http:") && !url.endsWith("file:")) {
                url = "http://" + url;
            }
        }
        webview.loadUrl(url);
    }
});

WebSettings设置

settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true); // 启用javascript脚本
settings.setBuiltInZoomControls(true); // 启用界面上放大和缩小按钮
settings.setUseWideViewPort(true); // 启用双击放大, 双击缩小功能
mWebView.loadUrl(url);

webSettings.setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);// 优先使用缓存
webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);//不使用缓存
webview.setHapticFeedbackEnabled(false); // 触觉回馈不可用
mWebSettings.setSupportZoom(true); // 支持缩放
webview.getSettings().setSavePassword(false); // 设置是否保存密码

webview加载资源

// webview加载本地资源
webview.loadUrl("file:///android_asset/demo.html");

// 加载web资源
webview.loadUrl("http://baidu.com");

// 这种方式可以解决中文乱码问题
String htmlContent="<html><head></head><body>少量内容,直接用字符串拼接</body></html>";
webview.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);

WebViewClient

使用webview打开网页

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        // 返回true用WebView打开,false调用系统浏览器或第三方浏览器
        return true;
    }
});

加载网页完成后,进度条消失

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        mProgressbar.setVisibility(View.GONE);
    }
});

总结

class MyWebViewClient extends WebViewClient {
    //设置使用webview打开网页
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    // 开始加载网页时要做的工作
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    // 加载完成时要做的工作
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    // 加载错误时要做的工作
    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        Log.d("onReceivedError", "error=" + description);
        Toast.makeText(MainActivity.this, errorCode + "/" + description,
                Toast.LENGTH_LONG).show();
    }

}

WebChromeClient

class MyWebChromeClient extends WebChromeClient {

    // 对话框
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
        Builder builder = new Builder(MainActivity.this);
        builder.setTitle("提示对话框");
        builder.setMessage("message");
        builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.confirm();
            }
        });
        builder.setCancelable(false);
        builder.create();
        builder.show();
        return true;
    }

    // 带按钮的对话框
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        Builder builder = new Builder(MainActivity.this);
        builder.setTitle("带选择的对话框");
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.confirm();
            }
        });
        builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.cancel();
            }
        });
        builder.setCancelable(false);
        builder.create();
        builder.show();
        return true;
    }

    // 带输入框的对话框
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
            final JsPromptResult result) {

        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        final View v = inflater.inflate(R.layout.prom_dialog, null);

        // 设置 TextView对应网页中的提示信息
        TextView TextView_PROM = (TextView) v.findViewById(R.id.TextView_PROM);
        TextView_PROM.setText(message);
        // 设置EditText对应网页中的输入框
        EditText EditText_PROM = (EditText) v.findViewById(R.id.EditText_PROM);
        EditText_PROM.setText(defaultValue);

        Builder builder = new Builder(MainActivity.this);
        builder.setTitle("带输入的对话框");
        builder.setView(v);
        builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String value = ((EditText) v.findViewById(R.id.EditText_PROM)).getText().toString();
                result.confirm(value);
            }
        });
        builder.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.cancel();
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                result.cancel();
            }
        });
        builder.create();
        builder.show();
        return true;
    }

    // 设置网页加载的进度条
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        MainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
        super.onProgressChanged(view, newProgress);
    }

    // 设置应用程序的标题
    @Override
    public void onReceivedTitle(WebView view, String title) {
        MainActivity.this.setTitle(title);
        super.onReceivedTitle(view, title);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值