How to create a WebView: 1. Add permission: The permission must be used in AndroidManifest.xml: "android.permission.INTERNET", otherwise there will be a Web page not available error. 2. Create a WebView instance: WebView webView = new WebView(this); 3. WebView basic settings: webview.getSettings().setJavaScriptEnabled(true); //Setting support for Javascript webView.getSettings().setBuiltInZoomControls(true); // Add a zoom button to the page webView.requestFocus(); //Touch focus works. If it is not set, when clicking the text input box of the webpage, the soft keyboard cannot pop up and it does not respond to some other events. webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); //Cancel the scroll bar 4. Set the web page to be displayed by WevView: Internet use: webView.loadUrl("http://www.google.com"); Local file use: webView.loadUrl ("file:///android_asset/XX.html"); The local file is stored in: assets file
5. If you want to click on the link, it will be handled by yourself, instead of responding to the link in the browser of the newly opened Android system.
Add an event listener object (WebViewClient) to WebView
and override some of the methods
shouldOverrideUrlLoading: the response to the hyperlink button in the web page.
When a connection is pressed, WebViewClient will call this method and pass parameters: pressed url
onLoadResource
onPageStart
onPageFinish
onReceiveError
onReceivedHttpAuthRequest
6. If you use the webview point link to jump to multiple pages, if you do not do anything, click the system "Back" key, the entire browser will call finish() to end itself. If you want the web page you browse to roll back instead of exiting the browser, you need to process and consume the Back event in the current Activity.
Override the onKeyDown(int keyCoder, KeyEvent event) method of the Activity class.
- public boolean onKeyDown(int keyCoder,KeyEvent event) {
- if ( webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK ) {
- webView.goBack(); //返回webView的上一页面
- return true;
- }
- return false;
- }
WebView相关问题注意:
Android的webView很强大,其实就是一个浏览器,你可以把它嵌入到你想要的位置,我这里遇到两个问题,一是怎么知道网页的加载进度,二是加载网页时,点击网页里面的链接还是在当前的webview里跳转,不想跳到浏览器那边。解决办法如下:
- webView.setWebChromeClient(new WebChromeClient() {
- public void onProgressChanged(WebView view, int progress) {
- setTitle("页面加载中 ... " + progress + "%");
- setProgress(progress * 100);
- if (progress == 100) {
- setTitle(R.string.app_name);
- }
- }
- });
- webView.setWebViewClient(new WebViewClient() {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- //设置点击网页里面的链接还是在当前的webview里跳转
- view.loadUrl(url);
- return true;
- }
- @Override
- public void onReceivedSslError(WebView view,
- SslErrorHandler handler, android.net.http.SslError error) {
- //设置webview处理https请求
- handler.proceed();
- }
- public void onReceivedError(WebView view,
- int errorCode, String description, String failingUrl) {
- //加载页面报错时的处理
- Toast.makeText(MainActivity.this,
- "Oh no! " + description, Toast.LENGTH_SHORT).show();
- }
- });