Skip to content

Commit

Permalink
add JavaScript 弹窗 #53
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jan 28, 2023
1 parent fa75890 commit aed267a
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 3 deletions.
98 changes: 97 additions & 1 deletion ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -181,6 +189,94 @@ public void onReceivedTitle(WebView view, String title) {
}
}

@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
if (onByWebChromeCallback != null && onByWebChromeCallback.onJsAlert(view, url, message, result)) {
return true;
}
Dialog alertDialog = new AlertDialog.Builder(view.getContext()).
setMessage(message).
setCancelable(false).
setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
result.confirm();
}
})
.create();
alertDialog.show();
return true;
}

@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
if (onByWebChromeCallback != null && onByWebChromeCallback.onJsConfirm(view, url, message, result)) {
return true;
}
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == Dialog.BUTTON_POSITIVE) {
result.confirm();
} else {
result.cancel();
}
}
};
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, listener)
.setNegativeButton(android.R.string.cancel, listener).show();
return true;
}

@Override
public boolean onJsPrompt(final WebView view, String url, final String message, final String defaultValue, final JsPromptResult result) {
if (onByWebChromeCallback != null && onByWebChromeCallback.onJsPrompt(view, url, message, defaultValue, result)) {
return true;
}
view.post(new Runnable() {
@Override
public void run() {
final EditText editText = new EditText(view.getContext());
editText.setText(defaultValue);
if (defaultValue != null) {
editText.setSelection(defaultValue.length());
}
float dpi = view.getContext().getResources().getDisplayMetrics().density;
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == Dialog.BUTTON_POSITIVE) {
result.confirm(editText.getText().toString());
} else {
result.cancel();
}
}
};
new AlertDialog.Builder(view.getContext())
.setTitle(message)
.setView(editText)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, listener)
.setNegativeButton(android.R.string.cancel, listener)
.show();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int t = (int) (dpi * 16);
layoutParams.setMargins(t, 0, t, 0);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
editText.setLayoutParams(layoutParams);
int padding = (int) (15 * dpi);
editText.setPadding(padding - (int) (5 * dpi), padding, padding, padding);
}
});
return true;
}

//扩展浏览器上传文件
//3.0++版本
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Expand Down
2 changes: 1 addition & 1 deletion ByWebView/src/main/java/me/jingbin/web/ByWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public Builder addJavascriptInterface(String interfaceName, Object interfaceObj)
}

/**
* @param onTitleProgressCallback 返回Title Progress
* @param onTitleProgressCallback 返回Title Progress ,横竖屏和弹框
*/
public Builder setOnTitleProgressCallback(OnTitleProgressCallback onTitleProgressCallback) {
this.mOnTitleProgressCallback = onTitleProgressCallback;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.jingbin.web;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
Expand All @@ -14,7 +15,6 @@
import android.webkit.WebViewClient;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;

import java.lang.ref.WeakReference;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package me.jingbin.web;

import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebView;

/**
* Created by jingbin on 2020/6/30.
*/
Expand Down Expand Up @@ -29,4 +33,31 @@ public void onProgressChanged(int newProgress) {
public boolean onHandleScreenOrientation(boolean isShow) {
return false;
}

/**
* JavaScript alert 警告框
* 默认返回false,使用代码里的默认处理
* 如果要手动处理,需要返回true!且需要执行 result.confirm();
*/
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return false;
}

/**
* JavaScript confirm 确认框
* 默认返回false,使用代码里的默认处理
* 如果要手动处理,需要返回true!且需要执行 result.confirm();
*/
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
return false;
}

/**
* JavaScript prompt 提示框
* 默认返回false,使用代码里的默认处理
* 如果要手动处理,需要返回true!且需要执行 result.confirm();
*/
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
return false;
}
}
13 changes: 13 additions & 0 deletions app/src/main/assets/java_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
("<br\>"+data);
}

function myOnJsAlert(){
window.alert("你好,我是一个警告框!");
}
function myOnJsConfirm(){
window.confirm("你好,我是一个确认框!");
}
function myOnJsPrompt(){
window.prompt("请输入姓名","张三");
}
</script>
</head>
<body>
Expand All @@ -26,6 +35,10 @@
<!--可以将android端传过来的数据,处理后,放在这里再传给android端-->
<li><a onClick="window.injectedObject.startFunction('我是网页传出来的数据')">点击调用java代码并传递参数</a></li><br/>

<li><a onClick="myOnJsAlert()">JavaScript alert 警告框</a><br/>
<li><a onClick="myOnJsConfirm()">JavaScript confirm 确认框</a><br/>
<li><a onClick="myOnJsPrompt()">JavaScript prompt 提示框</a><br/><br/><br/>

<div id="content">Java调用Js代码,内容显示: </div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.jingbin.webviewstudy.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
Expand All @@ -10,6 +13,8 @@
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -127,6 +132,28 @@ public void onReceivedTitle(String title) {
public boolean onHandleScreenOrientation(boolean isShow) {
return super.onHandleScreenOrientation(isShow);
}

/**
* 自定义实现 onJsAlert 方法,如果不自定义可不实现此方法
* 一定要执行 result.confirm();
*/
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
Dialog alertDialog = new AlertDialog.Builder(view.getContext()).
setTitle("自定义标题").
setMessage(message).
setCancelable(false).
setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
result.confirm();
}
})
.create();
alertDialog.show();
return true;
}
};

private OnByWebClientCallback onByWebClientCallback = new OnByWebClientCallback() {
Expand Down

0 comments on commit aed267a

Please sign in to comment.