webview
来源:互联网 发布:asp在线客服系统源码 编辑:程序博客网 时间:2024/06/05 11:34
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); }}
0 0
- WebView
- WebView
- WebView
- webView
- webView
- webview
- webview
- WebView
- webview
- webview
- webview
- WebView
- webView
- webview
- webView
- webview
- WebView
- WebView
- Android手机Root授权原理细节全解析
- 【Swift】学习笔记(六)——函数
- 省市区数组
- GRE写作必备句型
- FPGA图像处理-ISP之前言
- webview
- 为什么写测试用例?编写测试用例从何着手?测试用例包含什么内容?
- 如何向中级程序员转变
- 杭电 1860 统计字符
- 数据库创建
- 纯代码sizeclass使用
- Mybatis-Spring
- NSString / NSData / char* 类型之间的转换
- iOS_网络请求_代理方式