Android WebView的用法示例代码

manifest中需要添加访问网络的权限

[html]  view plain  copy
  1. <uses-permission android:name="android.permission.INTERNET" />  

MainActivity.Java

[java]  view plain  copy
  1. package com.example.webview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.Window;  
  8. import android.webkit.URLUtil;  
  9. import android.webkit.WebChromeClient;  
  10. import android.webkit.WebSettings;  
  11. import android.webkit.WebView;  
  12. import android.webkit.WebViewClient;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     WebView wv;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         getWindow().requestFeature(Window.FEATURE_PROGRESS);  
  24.           
  25.         setContentView(R.layout.activity_main);  
  26.         wv = (WebView)findViewById(R.id.wv);  
  27.         WebSettings ws = wv.getSettings();  
  28.         ws.setSupportZoom(true);  
  29.               ws.setBuiltInZoomControls(true);  
  30.         wv.setWebChromeClient(new WebChromeClient(){  
  31.             public void onProgressChanged(WebView view, int newProgress){  
  32.                 MainActivity.this.setProgress(newProgress*100);  
  33.             }  
  34.         });  
  35.         wv.setWebViewClient(new WebViewClient(){  
  36.             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)  
  37.             {  
  38.                 Toast.makeText(MainActivity.this"Sorry!" + description, Toast.LENGTH_SHORT).show();  
  39.             }  
  40.         });  
  41.         Button btn = (Button)findViewById(R.id.btn);  
  42.         btn.setOnClickListener(new OnClickListener(){  
  43.   
  44.             @Override  
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub  
  47.                 EditText et = (EditText)findViewById(R.id.et);  
  48.                 String url = et.getText().toString().trim();  
  49.                 if(URLUtil.isNetworkUrl(url)){  
  50.                     wv.loadUrl(url);  
  51.                 }  
  52.                 else{  
  53.                     Toast.makeText(MainActivity.this"对不起,您输入的网站有错。", Toast.LENGTH_SHORT).show();  
  54.                     et.requestFocus();  
  55.                 }  
  56.             }  
  57.               
  58.         });  
  59.           
  60.         Button btnForward = (Button)findViewById(R.id.btnForward);  
  61.         btnForward.setOnClickListener(new OnClickListener(){  
  62.   
  63.             @Override  
  64.             public void onClick(View v) {  
  65.                 // TODO Auto-generated method stub  
  66.                 if(wv.canGoForward()){  
  67.                     wv.goForward();  
  68.                 }  
  69.                 else{  
  70.                     Toast.makeText(MainActivity.this"对不起,您现在不能前进!", Toast.LENGTH_SHORT).show();  
  71.                 }  
  72.             }  
  73.               
  74.         });  
  75.           
  76.         Button btnBack = (Button)findViewById(R.id.btnBack);  
  77.         btnBack.setOnClickListener(new OnClickListener(){  
  78.   
  79.             @Override  
  80.             public void onClick(View v) {  
  81.                 // TODO Auto-generated method stub  
  82.                 if(wv.canGoBack())  
  83.                 {  
  84.                     wv.goBack();  
  85.                 }  
  86.                 else{  
  87.                     Toast.makeText(MainActivity.this"对不起,您现在不能后退!", Toast.LENGTH_SHORT).show();  
  88.                 }  
  89.             }             
  90.         });       
  91.     }  
  92.   
  93. }  


activity_main.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.       
  9.     <LinearLayout   
  10.         android:orientation="horizontal"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         >  
  14.           
  15.         <Button   
  16.             android:id="@+id/btnForward"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="@string/forward"  
  20.             />  
  21.         <Button   
  22.             android:id="@+id/btnBack"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="@string/back"  
  26.             />  
  27.         <EditText   
  28.             android:id="@+id/et"  
  29.             android:layout_width="175px"  
  30.             android:layout_height="wrap_content"  
  31.             android:singleLine="true"  
  32.             android:selectAllOnFocus="true"  
  33.             android:text="@string/et"  
  34.             />  
  35.         <Button   
  36.             android:id="@+id/btn"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_gravity="right"  
  40.             android:text="@string/btn"  
  41.             />  
  42.           
  43.     </LinearLayout>  
  44.       
  45.     <WebView   
  46.         android:id="@+id/wv"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         />  
  50.       
  51. </LinearLayout>  

[java]  view plain  copy
  1. wv.loadUrl("file:///android_asset/index.html");  
  2. mWebSettings = mWebView.getSettings();   
  3.             mWebSettings.setJavaScriptEnabled(true);   
  4.             mWebSettings.setBuiltInZoomControls(true);   
  5.             mWebSettings.setLightTouchEnabled(true);   
  6.             mWebSettings.setSupportZoom(true);   
  7.             mWebView.setHapticFeedbackEnabled(false);   

本地assets中的html文件加载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值