WebView学习

来源:互联网 发布:ta热分析软件 编辑:程序博客网 时间:2024/06/06 03:25

MiniBrowser.Java

[java] view plain copy
  1. public class MiniBrowser extends Activity {  
  2.   
  3.     EditText url;  
  4.     WebView showWebView;  
  5.     Button searchButton;  
  6.     String urlStr = null;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         url = (EditText) findViewById(R.id.url);  
  12.         showWebView = (WebView) findViewById(R.id.show);          
  13.         searchButton = (Button) findViewById(R.id.searchButton);  
  14.           
  15.         //真机测试时,保证在当前的Activity显示页面,而非自带浏览器显示  
  16.         showWebView.setWebViewClient(new WebViewClient(){  
  17.             @Override  
  18.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  19.                 // TODO Auto-generated method stub  
  20.                 //用WebView 载入页面  
  21.                 view.loadUrl(url);  
  22.                 return true;  
  23.             }  
  24.         });   
  25.           
  26.         //enable JavaScript   
  27.         showWebView.getSettings().setJavaScriptEnabled(true);  
  28.           
  29.         searchButton.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 // TODO Auto-generated method stub  
  34.                 urlStr = url.getText().toString();  
  35.                 showWebView.loadUrl(urlStr);  
  36.                   
  37.             }  
  38.         });  
  39.                   
  40.     }  
  41.     @Override  
  42.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  43.         // TODO Auto-generated method stub  
  44.         //按后退键可以跳到上级页面  
  45.         if(keyCode == KeyEvent.KEYCODE_BACK){  
  46.             showWebView.goBack();  
  47.             return true;  
  48.         }  
  49.         return super.onKeyDown(keyCode, event);  
  50.     }  
  51.       
  52. }  
mail.xml 布局文件

[html] view plain copy
  1. <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MiniBrowser" >  
  6.   
  7.     <EditText   
  8.         android:id="@+id/url"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:singleLine="true"  
  13.         android:layout_toLeftOf="@+id/searchButton"  
  14.         android:layout_alignTop="@+id/searchButton"  
  15.         android:layout_alignBottom="@+id/searchButton" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/searchButton"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentRight="true"  
  22.         android:text="@string/search" />  
  23.   
  24.      
  25.     <WebView   
  26.         android:id="@+id/show"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="fill_parent"  
  29.         android:layout_below="@id/searchButton"/>  
  30.  </RelativeLayout>  


Manife文件设置允许上网

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


在真机的测试结果



0 0
原创粉丝点击