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 >
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
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);
}
}
});
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);
}
}
});
}