Android 访问网页

 1. 配置网络访问权限 

  修改项目根目录中AndroidManifest.xml资源配置文件, 添加对Internet 访问权限:

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

全局 AndroidManifest.xml 清单如下: 

 <?xml version="1.0" encoding="utf-8"?>

< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
      package
="com.test.android"
      android:versionCode
="1"
      android:versionName
="1.0" >
    
< application  android:icon ="@drawable/icon"  android:label ="@string/app_name" >
        
< activity  android:name =".FirstAppUI"  android:label ="@string/app_name" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.MAIN"   />
                
< category  android:name ="android.intent.category.LAUNCHER"   />
            
</ intent-filter >
        
</ activity >
    
</ application >
    
< uses-permission  android:name ="android.permission.INTERNET" ></ uses-permission >
    
< uses-sdk  android:minSdkVersion ="8"   />
</ manifest >  

 

2. 修改 layout\main.xml 布局文件

 

<? xml version="1.0" encoding="utf-8" ?>
< AbsoluteLayout 
  
xmlns:android ="http://schemas.android.com/apk/res/android"
  android:orientation
="vertical"
  android:layout_width
="fill_parent"
  android:layout_height
="fill_parent"   >

 
< EditText
  
android:id ="@+id/editText01"  
  android:layout_width
="200px"
  android:layout_height
="40px"
  android:textSize
="18sp"
  android:layout_x
="5px"
  android:layout_y
="32px"   />
 
< Button
  
android:id ="@+id/button01"
  android:layout_width
="60px"
  android:layout_height
="40px"
  android:text
="转到"
  android:layout_x
="205px"
  android:layout_y
="35px"
  
/>
 <WebView 
   android:id ="@+id/webView01"  
  android:layout_height
="330px"  
  android:layout_width
="300px"  
  android:layout_x
="7px"
  android:layout_y
="90px"
  android:focusable
="false"
  
/> 

</AbsoluteLayout> 

 

3. MainActivity.java 的 onCreate() 方法中添加对网络的访问

 

private  Button button;
private  EditText editText;
private  WebView webView;

@Override
public   void  onCreate(Bundle savedInstanceState) {
    
super .onCreate(savedInstanceState);
    setContentView(R.layout.main);

    
//  获得布局中的控件
    button =  (Button) findViewById(R.id.button01);
    editText 
=  (EditText) findViewById(R.id.editText01);
    webView 
=  (WebView) findViewById(R.id.webView01);

    
//  查询按钮添加事件
    button.setOnClickListener( new  Button.OnClickListener() {
        
public   void  onClick(View arg0) {
            String strURL 
=  (editText.getText().toString());
            strURL 
=  strURL.trim();
            
            
if  (strURL.length()  ==   0 ) {
                Toast.makeText(MainActivity.
this " 查询内容不能为空! " ,Toast.LENGTH_LONG).show();
            }
            
else  {
                webView.loadUrl(strURL);
            }
        }
    });

转载于:https://www.cnblogs.com/Yuqui1980/archive/2011/08/02/2124989.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值