Android Open Source - jsWebView My Web Chrome Client2






From Project

Back to project page jsWebView.

License

The source code is released under:

Apache License

If you think the Android project jsWebView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package wjj.js;
/*from  w  ww.  j  a v a 2  s  . c om*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;

public class MyWebChromeClient2 extends WebChromeClient {
  Context context;

  public MyWebChromeClient2(Context context) {
    this.context = context;
  }

  // ??3?????????????????????????
  // ????javascript??alert
  @Override
  public boolean onJsAlert(WebView view, String url, String message,
      final JsResult result) {
    // ????builder?????????????
    Builder builder = new Builder(context);
    builder.setTitle("Alert ???");
    builder.setMessage(message);
    builder.setPositiveButton(android.R.string.ok,
        new AlertDialog.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            // ??????????????????????
            result.confirm();
          }
        });
    builder.setCancelable(false);
    builder.show();
    return true;
  }

  // ????javascript??confirm
  @Override
  public boolean onJsConfirm(WebView view, String url, String message,
      final JsResult result) {
    Builder builder = new Builder(context);
    builder.setTitle("Confirm ???");
    builder.setMessage(message);
    builder.setPositiveButton(android.R.string.ok,
        new AlertDialog.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            result.confirm();
          }
        });
    builder.setNegativeButton(android.R.string.cancel,
        new AlertDialog.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            result.cancel();// ??null
          }
        });
    builder.setCancelable(false);
    builder.show();
    return true;
  }

  @Override
  public boolean onJsPrompt(WebView view, String url, String message,
      String defaultValue, final JsPromptResult result) {
    // ??????????????TextView?Edittext???
    final LayoutInflater factory = LayoutInflater.from(context);
    final View dialogView = factory.inflate(R.layout.prom_dialog, null);
    // ??TextView??????????????
    ((TextView) dialogView.findViewById(R.id.TextView_PROM))
        .setText(message);
    // ??EditText?????????
    ((EditText) dialogView.findViewById(R.id.EditText_PROM))
        .setText(defaultValue);

    Builder builder = new Builder(context);
    builder.setTitle("Prompt ???" + url);
    builder.setView(dialogView);
    builder.setPositiveButton(android.R.string.ok,
        new AlertDialog.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            // ??????????????????????????
            String value = ((EditText) dialogView
                .findViewById(R.id.EditText_PROM)).getText()
                .toString();
            result.confirm(value);// ????value
          }
        });
    builder.setNegativeButton(android.R.string.cancel,
        new AlertDialog.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            result.cancel();// ????null
          }
        });
    builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
      public void onCancel(DialogInterface dialog) {
        result.cancel();
      }
    });
    builder.show();
    return true;
  }

  // ?????????????
  @Override
  public void onProgressChanged(WebView view, int newProgress) {
    ((Activity) context).getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
        newProgress);
    super.onProgressChanged(view, newProgress);
  }

  // ??????????title
  @Override
  public void onReceivedTitle(WebView view, String title) {
    ((Activity) context).setTitle(title);
    super.onReceivedTitle(view, title);
  }
}




Java Source Code List

wjj.js.MainActivity.java
wjj.js.MyWebChromeClient2.java
wjj.js.MyWebChromeClient.java