关闭

WebView学习

标签: webview
19人阅读 评论(0) 收藏 举报
分类:

在Activity上写的代码

public class MyWebViewActivity extends Activity {



private WebView mWebView;

private String url = "http://192.168.1.156/html/index.html";//自己本地


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.web_view_test);

mWebView = (WebView) findViewById(R.id.my_web_view);

     //连接网络,将URL设置到webview上

mWebView.loadUrl(url);

           

WebSettings setting = mWebView.getSettings();
setting.setJavaScriptEnabled(true);
setting.setCacheMode(WebSettings.LOAD_NO_CACHE);;

//将接口与Javascript关联

//红色部分就是相关联地代码;当点击喇叭图片时,弹出对话框

             mWebView.addJavascriptInterface(new MusicHtmlTest(), "musicServiceInterfaceName");

               //按返回键时,返回上一界面;而不是销毁activity
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
  mWebView.goBack();// 返回前一个页面
  return true;
 }
 return super.onKeyDown(keyCode, event);

}

//定义接口

class MusicHtmlTest{


public void playMusic(){
Toast.makeText(MyWebViewActivity.this, "播放音乐",
Toast.LENGTH_LONG).show();
}

}

访问界面代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>婚庆主页</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=2.0,user-scalable=yes">
<!--<meta name="viewport" content="width=640, initial-scale=0.5,maximum-scale=0.5" />-->
<meta name="apple-mobile-web-app-capable" content="YES">
<link rel="stylesheet" href="mycss.css" type="text/css" />
<script>
var i = 0;
var jh = "我们已婚!";

function myOnClick(){
//alert("你好,我是弹出对话框");
//document.write("javascript 学习");
//document.getElementById("sp11").innerHTML=jh+(i+=1);
window.musicServiceInterfaceName.playMusic();
}
function startGridView(){
window.musicServiceInterfaceName.startGridViewHttp();
}


function nativeToJs(){
var url = window.musicServiceInterfaceName.jsToNativeGetParameter(); //返回http://it.warmtel.com 
window.location.href=url; //跳转到指定url
}



function toHttpWeb(){
window.location.href="http://blog.warmtel.com";
}

</script>
</head>




<body >
 
<div class="wrapcontain" style="width: 300px;padding: 10px;padding-top: 0px;margin: 0 auto;background-color:#680011;">
<div class="imgTop">
<img class="imgtop_bg" src="./img/index_top_bg.png" /> 
<img class="imgtop_lb" src="./img/index_microphone.png" onclick="myOnClick()"/>
<h3 class="p3">
<p id="sp11" class="sp1">离我们婚礼</p>
<p class="sp2">
还有<span class="sp3">35</span>天
</p>
</h3>
</div>




<div class="imgShow">
  
<img src="./img/index_yaoqinghan.png" onclick="startGridView()"/> 
<img src="./img/index_zhufuqiang.png" onclick="nativeToJs()"/> 
<img src="./img/index_yaoyiyao.png" /> 
<img src="./img/index_hunsha.png" />
</div>


<div class="footer">
  
<a href="###">婚礼承办方:成都幸福公社婚庆公司<img
src="./img/redRightarrow.png" /></a>

</div>
</div>
   
<script src="demo.js"></script>
</body>
</html>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    阅读排行
    评论排行