enter image description here我在webview上的活动上使用webview,有一个后退按钮(不是键盘上的背压按钮),它是可点击的,但是它不工作。任何想法如何处理这个点击webview按钮。
CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(真);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog =
ProgressDialog.show(activity, Constants.EMPTY_VALUE, Constants.PLEASE_WAIT, true);
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, headers);
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (activity != null && progressDialog != null) {
progressDialog.setCancelable(false);
}
updatePaymentUrl = null;
if (url.contains("callback-data")) {
webView.setVisibility(View.GONE);
showpDialog();
int indexQ = url.indexOf("?");
partUrl = url.substring(indexQ + 1, url.length());
speedOrderUpdateApi();
}
if (updatePaymentUrl != null) {
super.onPageStarted(view, updatePaymentUrl, favicon);
} else {
super.onPageStarted(view, url, favicon);
}
}
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
Logger.logInfo("errorCode = ", String.valueOf(errorCode));
Logger.logInfo("description = ", description);
Logger.logInfo("failingUrl = ", failingUrl);
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onPageFinished(WebView view, String url) {
if (activity != null && progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
// If title not found hide webview
Logger.logInfo("view.getTitle() = ", view.getTitle());
if (view.getTitle() != null && "Not Found".equals(view.getTitle())) {
view.setVisibility(View.INVISIBLE);
Toast.makeText(activity, "Link Not Found", Toast.LENGTH_LONG).show();
}
}
}
});
// Not needed
Random random = new Random(System.currentTimeMillis());
int randomNumber = random.nextInt();
Logger.logInfo("urlTemp = ", url);
webView.loadUrl(url, headers);
}
答案 0 :(得分:0)
WebView
默认情况下没有OnBackPress
,而我们使用onKeyDown
来转到上一页,例如,您可以将此代码放入{{1活动
WebView
<强>更新强>
将此代码放入@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack(); // go back in only the web view
return true;
}
return super.onKeyDown(keyCode, event);
}
按钮
onClick()
答案 1 :(得分:0)
您可以使用assembleDebug
从您的网页与本地代码进行通信,
对于这些内容,您必须创建一个WebView.addJavaScriptInterface()
(将其命名为您想要的任何内容)并将其作为WebInterface
添加到您的JavascriptInterface
,
例如
WebView
现在将此界面添加到您的网页视图
public class YourWebInterface {
private Context context;
public YourWebInterface(Context context) {
this.context = context;
}
@JavascriptInterface
public void goBack() {
if(webView.canGoBack()){
webView.goBack();
} else {
finish();
}
}
}
现在在您的网页中,您可以使用webView.addJavascriptInterface(new YourWebInterface(context), "Android");
传递的Android
对象来调用此方法,
如果您点击网页上的按钮,可以执行以下操作:
Interface