Android Webview 带后退按钮,如果有的话

发布于 2024-11-11 03:40:35 字数 3148 浏览 5 评论 0原文

漠视。我打开了安装的错误应用程序。它的效果非常好。 :)

我的后退按钮在我的网络视图中正常工作,但我想知道一些事情。 如何让 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);
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三人与歌 2024-11-18 03:40:35

下面的代码对我有用

public void onBackPressed (){

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

The below code worked for me

public void onBackPressed (){

    if (webview.isFocused() && webview.canGoBack()) {
            webview.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}
望她远 2024-11-18 03:40:35

试试这个

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webView.canGoBack())
            {
                myWebView.goBack();
            }
            else
            {
                finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Try this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webView.canGoBack())
            {
                myWebView.goBack();
            }
            else
            {
                finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}
耳钉梦 2024-11-18 03:40:35

尝试下面的代码片段

public void back() {
    if (history.size() > 0) {
        history.remove(history.size() - 1);
        if (history.size() <= 0) {
            finish();
        } else {
            setContentView(history.get(history.size() - 1));
        }
    } else {
        finish();
    }
}

@Override
public void onBackPressed() {
    TopNewsGroup.group.back();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        TopNewsGroup.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

try the following snippet

public void back() {
    if (history.size() > 0) {
        history.remove(history.size() - 1);
        if (history.size() <= 0) {
            finish();
        } else {
            setContentView(history.get(history.size() - 1));
        }
    } else {
        finish();
    }
}

@Override
public void onBackPressed() {
    TopNewsGroup.group.back();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        TopNewsGroup.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
倾其所爱 2024-11-18 03:40:35

这是您需要的代码。

public void showAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle(R.string.alerta);
    adb.setMessage(getResources().getString(R.string.mserror));
    adb.setIcon((status) ? R.drawable.close : R.drawable.alerta);

    adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
        finish();
    } });

    adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        } });

    adb.show();
}

进而

public void onBackPressed(){

    if (webView.isFocused() && webView.canGoBack()) {
        webView.goBack();       
    }
    else {
        showAlertDialog(Web.this, "","", false);
    }
}

This is the code that you need.

public void showAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle(R.string.alerta);
    adb.setMessage(getResources().getString(R.string.mserror));
    adb.setIcon((status) ? R.drawable.close : R.drawable.alerta);

    adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
        finish();
    } });

    adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        } });

    adb.show();
}

And then

public void onBackPressed(){

    if (webView.isFocused() && webView.canGoBack()) {
        webView.goBack();       
    }
    else {
        showAlertDialog(Web.this, "","", false);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文