再次点击以通过导航抽屉退出应用。

huangapple go评论72阅读模式
英文:

Click again to exit from the app using Navigation drawer

问题

这是翻译好的部分:

private boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START) && doubleBackToExitPressedOnce) {
        super.onBackPressed();
    } else if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else if (!doubleBackToExitPressedOnce) {
        doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }
}

希望这对你有所帮助。如果有其他疑问,请随时提问。

英文:

I have a problem with this code. I wanna when the drawer is close and the user clicks back the drawer open, and when it opens and the user wanna to exit from the app show message said: "press again to exit". how can I do that with this code?

I wanna it be like this

press back -- open drawer;

press back again -- show message;

press back again -- exit the app;

Here's my code

private boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START) && doubleBackToExitPressedOnce) {
        super.onBackPressed();
    }

    else {
        drawer.openDrawer(GravityCompat.START);
    }
}

And here is the message code and I don't know where to but it

doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);

答案1

得分: 1

这应该解决你的问题。

    @Override
    public void onBackPressed() {
        // 如果导航抽屉尚未打开,打开它。
        if (!drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.openDrawer(GravityCompat.START);
        } else if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
        } else {
            doubleBackToExitPressedOnce = true;
            Toast.makeText(this, "再次按下返回键退出", Toast.LENGTH_SHORT).show();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    doubleBackToExitPressedOnce = false;
                }
            }, 2000);
        }
    }
英文:

This should solve your problem.

@Override
public void onBackPressed() {
    // If navigation drawer is not open yet, open it.
    if (!drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.openDrawer(GravityCompat.START);
    } else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
    } else {
        doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }
}

huangapple
  • 本文由 发表于 2020年10月15日 23:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64375388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定