Android学习之利用WebView打开网页

首先还是布局文件main.xml的Code:

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.         <LinearLayout   
  8.             android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         >  
  12.         <EditText  
  13.                 android:id="@+id/editText"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_weight="1.0"  
  17.             android:lines="1"  
  18.             />  
  19.          <Button  
  20.             android:id="@+id/button"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="go"  
  24.             />  
  25.          </LinearLayout>  
  26.           <WebView  
  27.             android:id="@+id/webView"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_weight="1.0"  
  31.             />  
  32. </LinearLayout>  

 

利用两个LineraLayout的嵌套显示,上面显示一个编辑框和按钮,下面的是webView用来显示网页,大家要注意两个LineraLayout的android:orientation的属性,最外面的这个是垂直显示,里面的是水平显示,最终得到效果如图:

 

效果图下面的白色区域就是webView,等Java的Code完成后就能看出效果啦,期待吧!呵呵!

 

[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.widget.*;  
  3. import android.os.Bundle;  
  4. import android.webkit.*;  
  5. import android.view.KeyEvent;  
  6. import android.view.View;  
  7. import android.view.View.*;  
  8. public class MyBrowserView extends Activity {  
  9.       
  10.     private EditText editText;  
  11.     private Button   button;  
  12.     private WebView  webView;  
  13.       
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         editText = (EditText) findViewById(R.id.editText);  
  21.         button   = (Button)   findViewById(R.id.button);  
  22.         webView  = (WebView)  findViewById(R.id.webView);  
  23.           
  24.         button.setOnClickListener(new OnClickListener() {  
  25.             //同样为按钮绑定点击事件  
  26.             public void onClick(View v) {  
  27.                 openBrowser();  
  28.             }  
  29.         });  
  30.           
  31.         editText.setOnKeyListener(new OnKeyListener() {  
  32.             //同样为编辑框绑定键盘事件  
  33.             @Override  
  34.             public boolean onKey(View v, int keyCode, KeyEvent event) {  
  35.                 // TODO Auto-generated method stub  
  36.                 if(keyCode==KeyEvent.KEYCODE_ENTER) {  
  37.                     openBrowser();  
  38.                     return true;  
  39.                 }  
  40.                 return false;  
  41.             }  
  42.         });  
  43.     }  
  44.     //利用webView的loadUrl方法  
  45.     public void openBrowser() {  
  46.         webView.loadUrl("http://"+editText.getText().toString());  
  47.     }  
  48. }  

 

除了openBrowser()方法,其他的和用Intent打开网页是差不多的。

只要利用webView的loadUrl()方法就能加载网页。好的什么也不说啦!看效果图:

 

 

 

 

哈哈,不错吧,这样就方便打开别的网页啦!哦!差点忘了,要在AndroidManifest.xml中加入

<uses-permission android:name="android.permission.INTERNET">

否则就不能访问Internet哟!

到这里看了上一篇的同学可能就问,为什么上个例子不要加上这条语句呢?

因为通过Intent就能请求其他应用程序查看网页,而其他应用程序需要在自己的AndroidManifest.xml中请求获得访问Internet的权限。

不知道解释的清楚不?呵呵!

好了今天的就到这里了哟!祝大家一起进步!加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值