WebView

布局:

 

<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" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >

<EditText
android:id="@+id/input_edittext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="请输入网址"/>

<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOTO"
android:layout_gravity="center_vertical"
android:gravity="center"/>

</LinearLayout>

 

<RelativeLayout
android:id="@+id/webview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">

<WebView
android:id="@+id/test_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

<ProgressBar
android:id="@+id/loading_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"/>

 

 </RelativeLayout>

 

代码:

package com.example.webviewdemo;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

@SuppressLint("SetJavaScriptEnabled")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends Activity {

private EditText mEditText;

private Button mButton;

private RelativeLayout mWebViewLayout;

private WebView mWebView;


private ProgressBar mProgressBar;

private WebViewClient mWebViewClient = new WebViewClient(){

@Override
public void onPageFinished(WebView view, String url) {
// 载入页面完成
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
mEditText.setText(url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// 载入页面开始
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 点击链接响应
view.loadUrl(url);
return true;
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mEditText = (EditText)findViewById(R.id.input_edittext);
mButton = (Button)findViewById(R.id.start_button);
mProgressBar = (ProgressBar)findViewById(R.id.loading_progressbar);
mWebViewLayout = (RelativeLayout)findViewById(R.id.webview_layout);
mWebView = (WebView)findViewById(R.id.test_webview);

//设置WebView是否支持JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);
//设置是否支持缩放
mWebView.getSettings().setSupportZoom(true);
//取消滚动条
mWebView.setScrollBarStyle(ScrollView.SCROLLBARS_OUTSIDE_OVERLAY);

//页面事件监听
mWebView.setWebViewClient(mWebViewClient);

mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//设置要显示的网页
mWebView.loadUrl(mEditText.getEditableText().toString());
}
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//浏览的网页回退
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
protected void onDestroy() {
super.onDestroy();
//资源释放
mWebViewLayout.removeAllViews();
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.removeAllViews();
mWebView.destroy();

}

}

 

posted @ 2016-05-24 09:50  Egg丶牛皮  阅读(234)  评论(0编辑  收藏  举报