将网页嵌入到android应用中

将网页嵌入到 android 应用中

 

[java]  view plain copy
  1. package com.android.webviewtest;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.KeyEvent;  
  5. import android.webkit.WebSettings;  
  6. import android.webkit.WebView;  
  7. import android.webkit.WebViewClient;  
  8.   
  9.   
  10. public class MainActivity extends Activity {  
  11.     private WebView myWebView ;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         //获取webview控件  
  17.         myWebView = (WebView) findViewById(R.id.webview);  
  18.         //加载服务器上的页面  
  19.         myWebView.loadUrl("http://www.baidu.com");  
  20.         //加载本地中的html  
  21.         //myWebView.loadUrl("file:///android_asset/www/test2.html");  
  22.         //加上下面这段代码可以使网页中的链接不以浏览器的方式打开  
  23.         myWebView.setWebViewClient(new WebViewClient());  
  24.         //得到webview设置  
  25.         WebSettings webSettings = myWebView.getSettings();    
  26.         //允许使用javascript  
  27.         webSettings.setJavaScriptEnabled(true);  
  28.         //将WebAppInterface于javascript绑定  
  29.         myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  
  30.           
  31.     }  
  32.       
  33.       
  34.     @Override  
  35.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  36.         // Check if the key event was the Back button and if there's history  
  37.         if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {  
  38.             myWebView.goBack();  
  39.             return true;  
  40.         }  
  41.       //  return true;  
  42.         // If it wasn't the Back key or there's no web page history, bubble up to the default  
  43.         // system behavior (probably exit the activity)  
  44.         return super.onKeyDown(keyCode, event);  
  45.     }  
  46.   
  47.   
  48.       
  49. }  

[java]  view plain copy
  1. package com.android.webviewtest;  
  2.   
  3. import android.content.Context;  
  4. import android.webkit.JavascriptInterface;  
  5. import android.widget.Toast;  
  6.   
  7. public class WebAppInterface {  
  8.     Context mContext;  
  9.   
  10.     /** Instantiate the interface and set the context */  
  11.     WebAppInterface(Context c) {  
  12.         mContext = c;  
  13.     }  
  14.   
  15.     /** Show a toast from the web page */  
  16.     @JavascriptInterface  
  17.     public void showToast(String toast) {  
  18.         Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();  
  19.     }  
  20. }  

html代码
[html]  view plain copy
  1. <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />  
  2. 这里调用android中的方法  
  3. <script type="text/javascript">  
  4.     function showAndroidToast(toast) {  
  5.         Android.showToast(toast);  
  6.     }  
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值