本文介绍了如何使用“后退"按钮返回CrossWalk的XWalkView或将其禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用以下代码返回webview.但是由于渲染能力低,我用XWalkView代替了WebView.

I use below code to go back in webview at first try. But for the low render ability, I used XWalkView replace the WebView.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    WebView mWebView = (WebView) findViewById(R.id.webview);
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:

                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                    if (MainActivity.mTencent.isSessionValid()) {
                        MainActivity.logout();
                    }
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

当更改为XWalkView时,我只能找到关于返回XWalkView.但是我找不到使用它的示例.如果我未实现后退按钮,则双击后退按钮后,应用程序将退出.

When change to XWalkView, I only find this about go back in the XWalkView. But I cannot find an example about to use it.When I not implement the back button envent, the app will exit after I double click the back button.

我的问题是:1.如何使用请回到XWalkView中,如果某些代码可能更有用.2.当我不使用返回功能时,如何禁用返回按钮单击事件.

My question is:1. How to use go back in the XWalkView, if some code may be more helpful.2. How can I disable the back button click event, when I not use the go back functon.

谢谢.

推荐答案

经过数天的挖掘,我解决了这个问题:将其放入活动xwalkview中.虽然可行,但有时会丢失一些历史记录.所以我也希望有人在这里给出更好的答案.

After days of digging, I solved this: put this in the activity xwalkview in. Though this works, but the go back sometimes lost some history. So I also want someone give a better answer here.

用于后退:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    //WebView mWebView = (WebView) findViewById(R.id.webview);
    XWalkView mXWalkView = (XWalkView) findViewById(R.id.xWalkView);
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:

                if (mXWalkView.getNavigationHistory().canGoBack()) {
                    mXWalkView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1) ;
                } else {
                    finish();
                    if (MainActivity.mTencent.isSessionValid()) {
                        MainActivity.logout();
                    }
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

对于禁用返回事件,您可以覆盖以下方法之一:dispatchKeyEventonBackPressedonKeyDown.有关更多信息,请参见此答案.

for disable back event you can override either of these method dispatchKeyEvent, onBackPressed, onKeyDown. Refer to this answer for more.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}

这篇关于如何使用“后退"按钮返回CrossWalk的XWalkView或将其禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:25