在WebView中按下后退按钮如何返回上一页?
我有一个应用程序,其中有一个 WebView,可以在其中显示一些网站。它有效,单击网页中的链接会转到我的应用程序内网站的下一页。但是当我点击手机的后退按钮时,它会直接进入我的应用程序。我想返回网站的上一页。我该怎么做?
这是我正在使用的代码示例:
public class Webdisplay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webdisplay);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
Toast loadingmess = Toast.makeText(this,
"Cargando El Diario de Hoy", Toast.LENGTH_SHORT);
loadingmess.show();
WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.elsalvador.com");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setInitialScale(1);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity = this;
myWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
}
I have an app in which I have a WebView
where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my app. But when I click the phone's back button, it takes me straight into my app. I want to go back to the previous page in the website instead. How can I do this?
Here is the code sample I'm using:
public class Webdisplay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webdisplay);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
Toast loadingmess = Toast.makeText(this,
"Cargando El Diario de Hoy", Toast.LENGTH_SHORT);
loadingmess.show();
WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.elsalvador.com");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setInitialScale(1);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity = this;
myWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
在 Kotlin 中 friederbluemle 的答案是:
In Kotlin friederbluemle's answer is:
使用此代码返回页面,当最后一页出现时,然后退出活动
use this code to go back on page and when last page came then go out of activity
我在 WebView 的活动中使用类似的内容:
编辑:
为了使此代码正常工作,您需要向包含 WebView 的
Activity
添加一个字段:在
onCreate()< 中初始化它/code> 方法,你应该可以开始了。
I use something like this in my activities with WebViews:
Edit:
For this code to work, you need to add a field to the
Activity
containing the WebView:Initialize it in the
onCreate()
method and you should be good to go.如果使用 Android 2.2 及更高版本(现在大多数设备),以下代码将满足您的需求。
If using Android 2.2 and above (which is most devices now), the following code will get you what you want.
这是我的解决方案。它也适用于 Fragment。
This is my solution. It works also in Fragment.
下一个按钮和进度条的完整参考:在 webview 中放置后退和下一个按钮
如果您愿意单击手机的后退按钮时转到后退页面,使用以下方法:
您还可以创建自定义后退按钮,如下所示:
Full reference for next button and progress bar : put back and next button in webview
If you want to go to back page when click on phone's back button, use this:
You can also create custom back button like this:
焦点也应该在 onBackPressed 中检查
Focusing should also be checked in onBackPressed
为什么不使用
onBackPressed()
?Why not use
onBackPressed()
?这是确认退出的代码:
here is a code with confirm exit:
在 kotlin 中:
webView - xml 中 webview 组件的 id(如果使用合成引用)。
In kotlin:
webView - id of the webview component in xml, if using synthetic reference.
我想我有点晚了,但根据 android 文档 ,您可以通过以下代码在片段内自定义后退导航。
您可以根据需要自定义 webView 无法返回时要执行的操作。
I think I'm a little late but according to the android documentation, you can have custom back navigation inside fragments done by the following piece of code.
you can customize what to do when you the webView cant go back as per your needs.
FoamyGuy 的第一个答案是正确的,但我有一些补充;低声誉不允许我发表评论。如果由于某些原因您的页面无法加载,请确保设置一个标记来记录失败,然后在 onBackPressed 覆盖中检查它。否则,您的 canGoBack() 将永远执行,而不会前往实际的返回活动(如果存在):
The first answer by FoamyGuy is correct but I have some additions; low reputations cannot allow me to do comments. If for some reasons your page fails to load, ensure that you set a flag to take note of the failure and then check it on the onBackPressed override. Otherwise your canGoBack() will be forever executed without heading to the actual back activity if it was there:
如果有人想在 fragment 中处理 webView 的 backPressed,那么他可以使用下面的代码。
将以下代码复制到您的
Activity
类中(包含片段YourFragmmentName
)<前><代码>@Override
公共无效onBackPressed(){
列表<片段> fragmentList = getSupportFragmentManager().getFragments();
布尔处理 = false;
for(对象 f:fragmentList) {
if(f 你的片段名称实例) {
处理 = ((YourFragmentName)f).onBackPressed();
如果(已处理){
休息;
}
}
}
如果(!处理){
super.onBackPressed();
}
}
将此代码复制到片段
YourFragmentName 中
Notes
Activity
应替换为您正在使用的实际 Acitivity 类。YourFragmentName
应替换为您的片段的名称。YourFragmentName
中声明webView
,以便可以从函数内访问它。If someone wants to handle backPressed for a webView inside a fragment, then he can use below code.
Copy below code into your
Activity
class (that contains a fragmentYourFragmmentName
)}
Copy this code in the fragment
YourFragmentName
Notes
Activity
should be replaced with the actual Acitivity class you are using.YourFragmentName
should be replaced with the name of your Fragment.webView
inYourFragmentName
so that it can be accessed from within the function.您可以在片段中尝试对 webview 进行此操作:
You can try this for webview in a fragment:
这是 Kotlin 的解决方案:
Here is the Kotlin solution:
您应该在类中使用以下库来处理 onBackKeyPressed。
canGoBack() 检查 webview 是否可以引用上一页。如果可能,请使用 goBack() 函数引用上一页(返回)。
You should the following libraries on your class handle the onBackKeyPressed.
canGoBack() checks whether the webview can reference to the previous page. If it is possible then use the goBack() function to reference the previous page (go back).
官方 Kotlin 方式:
https://developer.android.com/guide/webapps/ webview.html#NavigatingHistory
Official Kotlin Way:
https://developer.android.com/guide/webapps/webview.html#NavigatingHistory