Android的web视图与后退按钮,如果其他人 [英] Android Webview with Back Button, if else
本文介绍了Android的web视图与后退按钮,如果其他人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
否认。我打开安装了错误的应用程序。它奇妙的作品。 :)
我有后退按钮正常工作我的web视图中,但我想知道的东西。
如何使web视图回去,直到它再也看不到,而不是退出程序,把它打开了一个对话框,询问用户是否确保他们不会退出。
这里是我的作为在code。
感谢您抽出宝贵的时间。
.java文件
包com.vtd.whatthe;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.AlertDialog.Builder;
进口android.app.Dialog;
进口android.content.DialogInterface;
进口android.os.Bundle;
进口android.view.KeyEvent;
进口android.view.View;
进口android.widget.Toast;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
公共类WhatThe延伸活动{
私人的WebView的WebView;
/ **第一次创建活动时调用。 * /
公共无效onBack pressed(){
如果(webview.isFocused()&安培;&安培; webview.canGoBack()){
webview.goBack();
}
其他 {
openMyDialog(空);
}
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的WebView =(web视图)findViewById(R.id.webview);
webview.setWebViewClient(新HelloWebViewClient());
webview.getSettings()setJavaScriptEnabled(真)。
webview.setInitialScale(50);
。webview.getSettings()setUseWideViewPort(真正的);
webview.loadUrl(http://test2.com/);
}
私有类HelloWebViewClient扩展WebViewClient {
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
view.loadUrl(URL);
返回true;
}
}
公共无效openMyDialog(查看视图){
的ShowDialog(10);
}
@覆盖
受保护的对话框onCreateDialog(INT ID){
开关(ID){
案例10:
//创建我们的AlertDialog
建设者建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出吗?你有无限的猜测!)
.setCancelable(真)
.setPositiveButton(是,
新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话框,
其中INT){
//结束活动
WhatThe.this.finish();
}
})
.setNegativeButton(继续猜!,
新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话框,
其中INT){
Toast.makeText(getApplicationContext(),
祝你好运!,
Toast.LENGTH_SHORT).show();
}
});
返回builder.create();
}
返回super.onCreateDialog(ID);
}
}
解决方案
以下code为我工作。
公共无效onBack pressed(){
如果(webview.isFocused()&安培;&安培; webview.canGoBack()){
webview.goBack();
}
其他 {
super.onBack pressed();
完();
}
}
Disregard. I was opening the wrong app that was installed. It works wonderfully. :)
I have the back button working correctly within my webview, but I was wondering something.
How to make the webview go back until it cannot anymore, and instead of exiting the program, have it open up a dialog box asking if the user is sure they won't to exit.
Here is my take on the code.
Thanks for taking the time.
.java file
package com.vtd.whatthe;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WhatThe extends Activity {
private WebView webview;
/** Called when the activity is first created. */
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
openMyDialog(null);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://test2.com/");
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public void openMyDialog(View view) {
showDialog(10);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 10:
// Create our AlertDialog
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit? You have unlimited guesses!")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// Ends the activity
WhatThe.this.finish();
}
})
.setNegativeButton("Keep Guessing!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"Good Luck!",
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return super.onCreateDialog(id);
}
}
解决方案
The below code worked for me
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
super.onBackPressed();
finish();
}
}
这篇关于Android的web视图与后退按钮,如果其他人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文