import java.net.URI;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button, button_back, button_flash;
private WebView wv;
private TextView tv_show;
String path = "http://baidu.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 寻找控件
find_widget();
wv = (WebView) findViewById(R.id.webview);
// 按钮点击事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv.loadUrl(path);
}
});
// 刷新按监听事件
button_flash.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 刷新
Toast.makeText(MainActivity.this, "刷新", 3).show();
wv.reload();
}
});
// 刷新按钮监听事件
button_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "返回", 3).show();
finish();
}
});
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
// TODO Auto-generated method stub
tv_show.setText(title);
super.onReceivedTitle(view, title);
}
});
// wv的监听事件
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
//wv的下载监听事件
wv.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
// TODO Auto-generated method stub
if(url.endsWith(".apk")){
Uri uri=Uri.parse(url);
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
}
});
}
private void find_widget() {
button = (Button) findViewById(R.id.button1);
button_back = (Button) findViewById(R.id.button_back);
button_flash = (Button) findViewById(R.id.button2_flash);
tv_show = (TextView) findViewById(R.id.tv_show);
}
}
-------------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/line"
android:layout_centerHorizontal="true"
android:text="查看百度网页-webview" />
<LinearLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="返回" />
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="展示标题!" />
<Button
android:id="@+id/button2_flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="刷新" />
</LinearLayout>
</RelativeLayout>