您的位置:首页 > 移动开发

分享:android 源码——Webview开发常用知识点

2014-01-20 14:51 423 查看
        分享:android 源码——Webview开发常用知识点:

  [color=ound-color:rgb(247,]1、添加权限:AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错误。

  [color=ound-color:rgb(247,] 2、在要Activity中生成一个WebView组件:WebView webView = new WebView(this);

  [color=ound-color:rgb(247,] 3、设置WebView基本信息:

  [color=ound-color:rgb(247,] 如果访问的页面中有Javascript,则webview必须设置支持Javascript。

  [color=ound-color:rgb(247,] webview.getSettings().setJavaScriptEnabled(true);

  [color=ound-color:rgb(247,] 触摸焦点起作用

  [color=ound-color:rgb(247,] requestFocus();//如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件。

  [color=ound-color:rgb(247,] 取消滚动条

  [color=ound-color:rgb(247,] this.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);

  [color=ound-color:rgb(247,] 4、设置WevView要显示的网页:

  [color=ound-color:rgb(247,] 互联网用:webView.loadUrl("http://www.google.com");

  [color=ound-color:rgb(247,] 本地文件用:webView.loadUrl("file:///android_asset/XX.html"); 本地文件存放在:assets文件中

  [color=ound-color:rgb(247,] 5、如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。

  [color=ound-color:rgb(247,] 给WebView添加一个事件监听对象(WebViewClient)

  [color=ound-color:rgb(247,]

  [color=ound-color:rgb(247,] 并重写其中的一些方法

  [color=ound-color:rgb(247,] shouldOverrideUrlLoading:对网页中超链接按钮的响应。

  [color=ound-color:rgb(247,] 当按下某个连接时WebViewClient会调用这个方法,并传递参数:按下的url

  [color=ound-color:rgb(247,] onLoadResource

  [color=ound-color:rgb(247,] onPageStart

  [color=ound-color:rgb(247,] onPageFinish

  [color=ound-color:rgb(247,] onReceiveError

  [color=ound-color:rgb(247,] onReceivedHttpAuthRequest

  [color=ound-color:rgb(247,]

  [color=ound-color:rgb(247,] 6、如果用webview点链接看了很多页以后,如果不做任何处理,点击系统“Back”键,整个浏览器会调用finish()而结束自身,如果希望浏览的网页回退而不是退出浏览器,需要在当前Activity中处理并消费掉该Back事件。

  [color=ound-color:rgb(247,] 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法。

  [color=ound-color:rgb(247,] public boolean onKeyDown(int keyCoder,KeyEvent event){

  [color=ound-color:rgb(247,] if(webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){

  [color=ound-color:rgb(247,] webview.goBack(); //goBack()表示返回webView的上一页面

  [color=ound-color:rgb(247,] return true;

  [color=ound-color:rgb(247,] }

  [color=ound-color:rgb(247,] return false;

  [color=ound-color:rgb(247,] }

  [color=ound-color:rgb(247,]-----------------------------------------------------------------------------------------------------------------------------------

  [color=ound-color:rgb(247,]Android的webView很强大,其实就是一个浏览器,你可以把它嵌入到你想要的位置,我这里遇到两个问题,就是怎么知道网页的加载进度和加载网页时,

  [color=ound-color:rgb(247,]点击网页里面的链接还是在当前的webview里跳转,不想跳到浏览器那边,解决办法如下:

  [color=ound-color:rgb(247,]//此方法可以处理webview 在加载时和加载完成时一些操作

  [color=ound-color:rgb(247,]webView.setWebChromeClient(new WebChromeClient(){

  [color=ound-color:rgb(247,] @Override

  [color=ound-color:rgb(247,] public void onProgressChanged(WebView view, int newProgress) {

  [color=ound-color:rgb(247,] if(newProgress==100){ // 这里是设置activity的标题, 也可以根据自己的需求做一些其他的操作

  [color=ound-color:rgb(247,] title.setText("加载完成");

  [color=ound-color:rgb(247,] }else{

  [color=ound-color:rgb(247,] title.setText("加载中.......");

  [color=ound-color:rgb(247,] }

  [color=ound-color:rgb(247,] }

  [color=ound-color:rgb(247,]});

  [color=ound-color:rgb(247,]webView.setWebViewClient(new WebViewClient(){

  [color=ound-color:rgb(247,] @Override

  [color=ound-color:rgb(247,] public boolean shouldOverrideUrlLoading(WebView view, String url) { //重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边

  [color=ound-color:rgb(247,] view.loadUrl(url);

  [color=ound-color:rgb(247,] return true;

  [color=ound-color:rgb(247,] }

  [color=ound-color:rgb(247,] @Override

  [color=ound-color:rgb(247,]public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) { // 重写此方法可以让webview处理https请求

  [color=ound-color:rgb(247,]handler.proceed();

  [color=ound-color:rgb(247,]}

  [color=ound-color:rgb(247,]});

  [color=ound-color:rgb(247,]附件是详细的说明,和webview的一些其他的用途,比如: 和网页中的javascr进行交互等

  想要了解更多有关android 源码的知识可以查询:天地会。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: