我希望我的应用中加载的每个页面都通过url覆盖方法(我做一些html修改)。我怎样才能做到这一点?。
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do some html modifcations (myhtml)
view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack(); // go back 1 step
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:1)
在页面完成的方法中,您将获得该网址,您可以重新加载网址
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
_dialog.dismiss();
}
@Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
try{
_dialog.dismiss();
Toast.makeText(Showwebviewlatestnews.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
}
}
答案 1 :(得分:0)
尝试返回shouldOverrideUrlLoading
true
这是一个解释: -
如果主机应用程序想要离开当前的WebView并处理URL本身,则为True,否则返回false。 Reference
所以这应该可以解决问题,
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do some html modifcations (myhtml)
view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
return true;
}
并处理backPressed()
,例如 -
@Override
public void onBackPressed() {
if (webview != null && webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
答案 2 :(得分:0)
//替换此
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack(); // go back 1 step
return true;
}
return super.onKeyDown(keyCode, event);}
使用
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack(); // go back 1 step
web1.goBack();
return true;
}
return super.onKeyDown(keyCode, event);}