Android与Javascript交互示例(三)
MainAcivity如下:
package cn.testjavascriptdialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.LayoutInflater; import android.view.View; import android.webkit.JsPromptResult; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.EditText; /** * Demo描述: * 测试Android响应Javascript的三种对话框 * * 参考资料: * http://www.cnblogs.com/luckjun/archive/2013/03/22/2976117.html */ public class MainActivity extends Activity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mWebView=(WebView) findViewById(R.id.webView); initWebViewSettings(); // 为WebView设置WebChromeClient mWebView.setWebChromeClient(new WebChromeClientSubClass()); //加载本地Html文件 mWebView.loadUrl("file:///android_asset/test.html"); } private void initWebViewSettings(){ mWebView.setVerticalScrollBarEnabled(false); mWebView.setHorizontalScrollBarEnabled(false); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setPluginsEnabled(true); mWebView.requestFocus(); mWebView.getSettings().setUseWideViewPort(true); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setBuiltInZoomControls(true); } private class WebChromeClientSubClass extends WebChromeClient{ @Override public boolean onJsAlert(WebView view, String url, String message,final JsResult result) { Dialog alertDialog=null; AlertDialog.Builder builder=new Builder(MainActivity.this); builder.setTitle("Javascript的alert对话框"); builder.setMessage(message); builder.setNegativeButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { System.out.println("现在显示的是Javascript的alert对话框"); //注意: //必须要这一句代码:result.confirm()表示: //处理结果为确定状态同时唤醒WebCore线程 //否则不能继续点击按钮 result.confirm(); } }); alertDialog=builder.create(); alertDialog.show(); return true; } @Override public boolean onJsConfirm(WebView view, String url, String message,final JsResult result) { Dialog confirmDialog=null; AlertDialog.Builder builder=new Builder(MainActivity.this); builder.setTitle("Javascript的confirm对话框"); builder.setMessage(message); builder.setPositiveButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { System.out.println("现在点击的是Javascript的confirm对话框的OK"); //处理结果为确定状态同时唤醒WebCore线程 result.confirm(); } }); builder.setNegativeButton("NO", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { System.out.println("现在点击的是Javascript的confirm对话框的NO"); //处理结果为取消状态同时唤醒WebCore线程 result.cancel(); } }); confirmDialog=builder.create(); confirmDialog.show(); return true; } @Override public boolean onJsPrompt(WebView view, String url, String message,String defaultValue, final JsPromptResult result) { LayoutInflater inflater = getLayoutInflater(); View promptView = inflater.inflate(R.layout.prompt, null); final EditText nameEditText = (EditText) promptView.findViewById(R.id.nameEditText); nameEditText.setHint("请输入您的名字"); Dialog promptDialog=null; AlertDialog.Builder builder=new Builder(MainActivity.this); builder.setView(promptView); builder.setTitle("Javascript的prompt对话框"); builder.setPositiveButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { System.out.println("现在点击的是Javascript的prompt对话框的OK"); //处理结果为取消状态且唤醒WebCore线程 //同时弹出alert对话框提示用户 result.confirm(nameEditText.getText().toString()); } }); builder.setNegativeButton("NO", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { System.out.println("现在点击的是Javascript的prompt对话框的NO"); //处理结果为取消状态同时唤醒WebCore线程 result.cancel(); } }); promptDialog=builder.create(); promptDialog.show(); return true; } } }
main.xml如下:
<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" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试Android响应Javascript的三种对话框" android:layout_centerHorizontal="true" /> <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/textView" /> </RelativeLayout>
prompt.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:id="@+id/nameEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
test.html如下:
<html> <head> <script type="text/javascript"> function doAlert() { alert("hello!"); } function doConfirm() { confirm("are you sure?"); } function doPrompt() { var val = prompt("what's your name?"); if (val) { alert("your name is:" + val); } } function success(msg) { alert(msg); } </script> </head> <body background="black"> <br> <br> <input type="button" value="alert" onclick="doAlert()"/><br/> <br> <br> <input type="button" value="confirm" onclick="doConfirm()"/><br/> <br> <br> <input type="button" value="prompt" onclick="doPrompt()"/><br/> <br> <br> <div id="result"></div> </body> </html>