根据用户选择覆盖 webview url
override webview url depending on user selection
我遇到了一个我认为很常见的情况,但我很难找到解决方案。 This 是我发现的与我的问题相似的内容,但我不明白该解决方案如何为他工作。谁能指出正确的方法或解释正确的答案。
如果用户说 yes
,我只是想覆盖 webview 的 url,否则不覆盖,这是我的一段代码
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final boolean result[] = new boolean[1];
builder.setTitle("Confirm");
builder.setMessage("Are you sure to Finish Process?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//some code
result[0] = true;
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// some code
result[0] = false;
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
return result[0];
}
我需要 webview 等待用户响应,这现在没有发生,并且立即返回最初为 false 的布尔值 result[0]
。
如有遗漏,请指出。
AlertDialog 不会阻塞 UI 线程。
当您的 AlertDialog 显示时,它不会阻止 WebView 继续使用新的 url.
更新 UI
您需要先阻止加载 url,然后在用户做出决定后再次调用 webview.loadUrl
。
private WebView mWebView;
private boolean mUserHasAllowedUrlLoading = false;
private String mUrl;
...
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Store the url
mUrl = url;
// Keep the WebView so we can access it again in the AlertDialog's anonymous inner classes
mWebView = view;
if (!mUserHasAllowedUrlLoading) {
// This is the first time the url has attempted to load, so show the dialog.
// Build and show AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure to Finish Process?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mUserHasAllowedUrlLoading = true;
// Start loading the stored url again
mWebView.loadUrl(mUrl);
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
// We return true here so the WebView does not load anything for the moment
return true;
} else {
// We can only get here if the user has already seen the dialog and clicked the positive button.
// Reset the boolean so it's ready for next time the use tries to navigate away from the current page.
mUserHasAllowedUrlLoading = false;
// We return false this time so that the WebView actually loads the supplied url.
return false;
}
}
我遇到了一个我认为很常见的情况,但我很难找到解决方案。 This 是我发现的与我的问题相似的内容,但我不明白该解决方案如何为他工作。谁能指出正确的方法或解释正确的答案。
如果用户说 yes
,我只是想覆盖 webview 的 url,否则不覆盖,这是我的一段代码
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final boolean result[] = new boolean[1];
builder.setTitle("Confirm");
builder.setMessage("Are you sure to Finish Process?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//some code
result[0] = true;
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// some code
result[0] = false;
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
return result[0];
}
我需要 webview 等待用户响应,这现在没有发生,并且立即返回最初为 false 的布尔值 result[0]
。
如有遗漏,请指出。
AlertDialog 不会阻塞 UI 线程。
当您的 AlertDialog 显示时,它不会阻止 WebView 继续使用新的 url.
您需要先阻止加载 url,然后在用户做出决定后再次调用 webview.loadUrl
。
private WebView mWebView;
private boolean mUserHasAllowedUrlLoading = false;
private String mUrl;
...
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Store the url
mUrl = url;
// Keep the WebView so we can access it again in the AlertDialog's anonymous inner classes
mWebView = view;
if (!mUserHasAllowedUrlLoading) {
// This is the first time the url has attempted to load, so show the dialog.
// Build and show AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure to Finish Process?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mUserHasAllowedUrlLoading = true;
// Start loading the stored url again
mWebView.loadUrl(mUrl);
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
// We return true here so the WebView does not load anything for the moment
return true;
} else {
// We can only get here if the user has already seen the dialog and clicked the positive button.
// Reset the boolean so it's ready for next time the use tries to navigate away from the current page.
mUserHasAllowedUrlLoading = false;
// We return false this time so that the WebView actually loads the supplied url.
return false;
}
}