英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论