Android WebView加载HTML表单并通过javascript提交

http://www.eoeandroid.com/thread-90393-1-1.html


使用android的WebView控件加载HTML表单,通过javascript调用java对象提交表单,在java对象中获取表单的值:

java代码:
  1. import android.app.Activity;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.webkit.JsResult;
  8. import android.webkit.WebChromeClient;
  9. import android.webkit.WebSettings;
  10. import android.webkit.WebView;
  11. import android.widget.Button;
  12. import android.widget.TextView;

  13. public class WebViewTest extends Activity {
  14. private WebView mWebView = null;
  15. private TextView txtView = null;
  16. private Handler mHandler = new Handler();
  17. /** Called when the activity is first created. */

  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mWebView = (WebView) findViewById(R.id.webView);
  23. txtView = (TextView) findViewById(R.id.webViewResult);
  24. WebSettings webSettings = mWebView.getSettings();
  25. //不保存密码
  26. webSettings.setSavePassword(false);
  27. //不保存表单数据
  28. webSettings.setSaveFormData(false);
  29. webSettings.setJavaScriptEnabled(true);
  30. //不支持页面放大功能
  31. webSettings.setSupportZoom(false);
  32. mWebView.addJavascriptInterface(new LoginJavaScriptImpl(), "loginImpl"); mWebView.setWebChromeClient(new MyAndroidWebClient());

  33. ((Button)findViewById(R.id.btnLoadhtml)).setOnClickListener(new View.OnClickListener() {
  34. public void onClick(View arg0) {
  35. mWebView.loadData(createWebForm(), "text/html", "UTF-8");
  36. // mWebView.loadDataWithBaseURL("", createWebForm(), "text/html", "UTF-8", "");
  37. }
  38. });
  39. }

  40. private String returnValue;
  41. protected final class LoginJavaScriptImpl {
  42. public void login(String username, String password){
  43. returnValue = username + ": " + password; mHandler.post(new Runnable() {
  44. public void run() { txtView.setText(returnValue);
  45. }
  46. });
  47. }
  48. }

  49. private final class MyAndroidWebClient extends WebChromeClient {

  50. @Override
  51. public boolean onJsAlert(WebView view,String url, String message, JsResult result) {

java代码:

  1. new AlertDialog.Builder(WebViewTest.this) .setTitle("提示信息") .setMessage(message) .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  2. public void onClick( DialogInterface dialoginterface, int i) { } }).show();
  3. return true;
  4. }
  5. }
  6. private String createWebForm(){ StringBuffer sb = new StringBuffer();
  7. sb.append("< html>").append("< head>");
  8. sb.append("< meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"); sb.append("< title>").append("表单测试").append("< /title>");
  9. sb.append("< /head>< script language=\"javascript\">");
  10. sb.append("function checkform(){var username=document.loginForm.username.value;"); sb.append("var password=document.loginForm.password.value;");
  11. sb.append("if(username==\"\"){alert(\"用户名不能为空!\");
  12. return false
  13. ;}");
  14. sb.append("if(password==\"\"){alert(\"密码不能为空!\");
  15. return false;}");
  16. //sb.append("return username + \":\" + password;");
  17. sb.append("window.loginImpl.login(username, password)");
  18. sb.append("}");
  19. sb.append("< /script>");
  20. sb.append("< body>");
  21. sb.append("< form method=\"post\" name=\"loginForm\">");
  22. sb.append("< table>");
  23. sb.append("< tr>");
  24. sb.append("< td align=\"right\">").append("用户名").append("< /td>");
  25. sb.append("< td").append("< input type=\"text\" name=\"username\">").append("< /td>"); sb.append("< /tr>");
  26. sb.append("< tr>");
  27. sb.append("< td align=\"right\">").append("密 码").append("< /td>");
  28. sb.append("< td").append("< input type=\"password\" name=\"password\">").append("< /td>"); sb.append("< /tr>");
  29. sb.append("< tr>");
  30. sb.append("< td align=\"center\" colspan=\"2\">");
  31. sb.append("< input type=\"submit\" value=\"登录\" onclick=\"checkform();\">");
  32. sb.append(" < input type=\"reset\" value=\"重置\">").append("< /td>");
  33. sb.append("</tr>"); sb.append("< /table>"); sb.append("< /form>");
  34. sb.append("< /body>");
  35. sb.append("< /html>");
  36. return sb.toString();
  37. }
  38. }

  39. import android.app.Activity;
  40. import android.app.AlertDialog;
  41. import android.content.DialogInterface;
  42. import android.os.Bundle;
  43. import android.os.Handler;
  44. import android.view.View;
  45. import android.webkit.JsResult;
  46. import android.webkit.WebChromeClient;
  47. import android.webkit.WebSettings;
  48. import android.webkit.WebView;
  49. import android.widget.Button;
  50. import android.widget.TextView;
java代码:

  1. public class WebViewTest extends Activity {
  2. private WebView mWebView = null;
  3. private TextView txtView = null;
  4. private Handler mHandler = new Handler();
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. mWebView = (WebView) findViewById(R.id.webView);
  11. txtView = (TextView) findViewById(R.id.webViewResult);
  12. WebSettings webSettings = mWebView.getSettings();
  13. //不保存密码
  14. webSettings.setSavePassword(false);
  15. //不保存表单数据
  16. webSettings.setSaveFormData(false);
  17. webSettings.setJavaScriptEnabled(true);
  18. //不支持页面放大功能
  19. webSettings.setSupportZoom(false);
  20. mWebView.addJavascriptInterface(new LoginJavaScriptImpl(), "loginImpl"); mWebView.setWebChromeClient(new MyAndroidWebClient());
  21. ((Button)findViewById(R.id.btnLoadhtml)).setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View arg0) {
  23. mWebView.loadData(createWebForm(), "text/html", "UTF-8");
  24. // mWebView.loadDataWithBaseURL("", createWebForm(), "text/html", "UTF-8", "");
  25. }
  26. });
  27. }
  28. private String returnValue;
  29. protected final class LoginJavaScriptImpl {
  30. public void login(String username, String password){
  31. returnValue = username + ": " + password; mHandler.post(new Runnable() {
  32. public void run() { txtView.setText(returnValue);
  33. }
  34. });
  35. }
  36. }
  37. private final class MyAndroidWebClient extends WebChromeClient {

  38. @Override
  39. public boolean onJsAlert(WebView view,String url, String message, JsResult result) {
  40. new AlertDialog.Builder(WebViewTest.this) .setTitle("提示信息") .setMessage(message) .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  41. public void onClick( DialogInterface dialoginterface, int i) { }
  42. }).show();
  43. return true;
  44. }

  45. }





展开阅读全文

没有更多推荐了,返回首页