Android WebView的使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<wbr><wbr><wbr>android:orientation="vertical" android:layout_width="fill_parent"<br><wbr><wbr><wbr>android:layout_height="fill_parent"&gt;<br><wbr><wbr><wbr>&lt;TextView android:layout_width="fill_parent"<br><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height="wrap_content" android:text="@string/hello" /&gt;<br><wbr><wbr><wbr>&lt;EditText android:hint="请输入URL" android:id="@+id/EditText01"<br><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width="fill_parent" android:layout_height="wrap_content"&gt;&lt;/EditText&gt;<br><wbr><wbr><wbr>&lt;Button android:text="OK" android:id="@+id/Button01"<br><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width="fill_parent" android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br><wbr><wbr><wbr>&lt;WebView android:id="@+id/WebView" android:layout_width="fill_parent"<br><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height="wrap_content"&gt;&lt;/WebView&gt;<br> &lt;/LinearLayout&gt;<br><br><br><br><br> package com.Aina.Android;<br><br> import android.app.Activity;<br> import android.app.AlertDialog;<br> import android.app.AlertDialog.Builder;<br> import android.content.DialogInterface;<br> import android.os.Bundle;<br> import android.view.KeyEvent;<br> import android.view.LayoutInflater;<br> import android.view.View;<br> import android.view.Window;<br> import android.webkit.JsPromptResult;<br> import android.webkit.JsResult;<br> import android.webkit.URLUtil;<br> import android.webkit.WebChromeClient;<br> import android.webkit.WebSettings;<br> import android.webkit.WebView;<br> import android.webkit.WebViewClient;<br> import android.widget.Button;<br> import android.widget.EditText;<br> import android.widget.TextView;<br><br> public class Test extends Activity {<br><wbr><wbr><wbr><br><wbr><wbr><wbr> private EditText et = null;<br><wbr><wbr><wbr> private Button btn = null;<br><wbr><wbr><wbr> private WebView wv = null;<br><wbr><wbr><wbr> private WebSettings ws = null;<br><wbr><wbr><wbr> @Override<br><wbr><wbr><wbr> public void onCreate(Bundle savedInstanceState) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> super.onCreate(savedInstanceState);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> setContentView(R.layout.main);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> et = (EditText) this.findViewById(R.id.EditText01);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> btn = (Button) this.findViewById(R.id.Button01);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> wv = (WebView) this.findViewById(R.id.WebView);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ws = wv.getSettings();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ws.setAllowFileAccess(true);//设置允许访问文件数据<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ws.setJavaScriptEnabled(true);//设置支持javascript脚本<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ws.setBuiltInZoomControls(true);//设置支持缩放<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> wv.setWebViewClient(new WebViewClient(){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> public boolean shouldOverrideUrlLoading<wbr>(WebView view,String url){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> //当有新连接时,使用当前的 WebView<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> view.loadUrl(url);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return true;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> });<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> wv.setWebChromeClient(new WebChromeClient(){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> public boolean onJsAlert(WebView view,String url,String message,final JsResult result){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> //构建一个Builder来显示网页中的alert对话框<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> Builder builder = new Builder(Test.this);<br><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(android)