1.首先上布局main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入网址:" />
<EditText
android:id="@+id/webaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载资源"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载数据"
/>
</LinearLayout>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
2.上一下所需的活动WebViewActivity.java
package com.wang;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class WebViewActivity extends Activity {
private WebView webview;
private EditText webaddress;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webview);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webaddress=(EditText)findViewById(R.id.webaddress);
String path=webaddress.getText().toString();
//在webView中输入网址,http开头
webview.loadUrl(path);
}
});
Button button1=(Button)findViewById(R.id.bt2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webaddress=(EditText)findViewById(R.id.webaddress);
String data="<a href='http://www.baidu.com'>baidu</a>";
webview.loadData(data, "text/html", "utf-8");
}
});
}
}
3。亲,别忘了权限哦!<uses-permission android:name="android.permission.INTERNET"/>
4..运行经过如下: