带有后退按钮的 Android Webview,否则

标签 android button dialog webview back

无视。我打开了安装错误的应用程序。它工作得很好。 :)

我的 WebView 中的后退按钮可以正常工作,但我想知道一些事情。 如何让 webview 返回到它不能再返回,而不是退出程序,让它打开一个对话框,询问用户是否确定他们不会退出。 这是我对代码的看法。
感谢您抽出宝贵时间。

.java文件

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);
    }
}

最佳答案

下面的代码对我有用

public void onBackPressed (){

    if (webview.isFocused() && webview.canGoBack()) {
            webview.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}

关于带有后退按钮的 Android Webview,否则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6220602/

相关文章:

android - list 中等效的连接操作广播接收器

netbeans - 如何在 Netbeans 工具栏中创建自定义按钮?

android - MediaPlayer 在 release() 后崩溃; .. LogCat中没有 'Caused by',我可以调试吗?

javascript - Ember.js 1.5.1 绑定(bind)属性禁用到按钮

css - Bootstrap 按钮和加载动画

java - 在这个例子中我将如何抑制打印对话框?

java - 在输入字段中按 Enter 时关闭自定义对话框

java - 有谁知道如何摆脱这个 Eclipse 错误窗口?

android - 在动态模块中使用样式时出错 - android app bundle

Android AsyncTasks 如何检查 Activity 是否仍在运行