为了先体验一下效果,截了一个图
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:hint="请输入URL" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <Button android:text="OK" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <WebView android:id="@+id/WebView" android:layout_width="fill_parent" android:layout_height="wrap_content"></WebView> </LinearLayout>
package com.Aina.Android; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.webkit.JsPromptResult; import android.webkit.JsResult; import android.webkit.URLUtil; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Test extends Activity { /** Called when the activity is first created. */ private EditText et = null; private Button btn = null; private WebView wv = null; private WebSettings ws = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et = (EditText) this.findViewById(R.id.EditText01); btn = (Button) this.findViewById(R.id.Button01); wv = (WebView) this.findViewById(R.id.WebView); ws = wv.getSettings(); ws.setAllowFileAccess(true);//设置允许访问文件数据 ws.setJavaScriptEnabled(true);//设置支持javascript脚本 ws.setBuiltInZoomControls(true);//设置支持缩放 wv.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view,String url){ //当有新连接时,使用当前的 WebView view.loadUrl(url); return true; } }); wv.setWebChromeClient(new WebChromeClient(){ public boolean onJsAlert(WebView view,String url,String message,final JsResult result){ //构建一个Builder来显示网页中的alert对话框 Builder builder = new Builder(Test.this); builder.setTitle("提示对话框"); builder.setMessage(message); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub result.confirm(); } }); builder.setCancelable(false); builder.create(); builder.show(); return true; } public boolean onJsConfirm(WebView view,String url,String message,final JsResult result){ Builder builder = new Builder(Test.this); builder.setTitle("带选择的对话框"); builder.setMessage(message); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub result.confirm(); } }); builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub result.cancel(); } }); builder.setCancelable(false); builder.create(); builder.show(); return true; } public boolean onJsPrompt(WebView view,String url,String message,String defaultValue,final JsPromptResult result){ LayoutInflater inflater = LayoutInflater.from(Test.this); final View v = inflater.inflate(R.layout.prom_dialog, null); //设置 TextView对应网页中的提示信息 ((TextView)v.findViewById(R.id.TextView_PROM)).setText(message); //设置EditText对应网页中的输入框 ((EditText)v.findViewById(R.id.EditText_PROM)).setText(defaultValue); Builder builder = new Builder(Test.this); builder.setTitle("带输入的对话框 "); builder.setView(v); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String value = ((EditText)v.findViewById(R.id.EditText_PROM)).getText().toString(); result.confirm(value); } }); builder.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub result.cancel(); } }); builder.setOnCancelListener(new DialogInterface.OnCancelListener(){ @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub result.cancel(); } }); builder.create(); builder.show(); return true; } //设置网页加载的进度条 public void onProgressChanged(WebView view,int newProgress){ Test.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100); super.onProgressChanged(view, newProgress); } //设置应用程序的标题 public void onReceivedTitle(WebView view,String title){ Test.this.setTitle(title); super.onReceivedTitle(view, title); } }); btn.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub String url = et.getText().toString(); //判断输入的内容是不是网址 if(URLUtil.isNetworkUrl(url)){ wv.loadUrl(url); }else{ et.setHint("输入的网址不合法,请重新输入"); // et.setText("输入的网址不合法,请重新输入"); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK && wv.canGoBack()){ wv.goBack();//返回前一个页面 return true; } return super.onKeyDown(keyCode, event); } }
prom_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center_horizontal" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/TextView_PROM" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/EditText_PROM" android:layout_width="fill_parent" android:layout_height="wrap_content" android:selectAllOnFocus="true" android:scrollHorizontally="true"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Aina.Android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Test" 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" /> </manifest>
注意不要忘记加上网络权限啊!
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <html> <head> <title>index.jsp</title> <script type="text/JavaScript"> function s(){ alert("Good Morning!"); } function d(){ confirm("Are you ok?") } function f(){ prompt("What’s your name?") } </script> </head> <body> 下面我们演示三种对话框 <br/><br/> <input type="button" value="警告,提醒对话框" onclick="s()"> <br/><br/> <input type="button" value="带选择的对话框" onclick="d()"> <br/><br/> <input type="button" value="要求用户输入的对话框" onclick="f()"> </body> </html>