gpt4 book ai didi

java - 菜单打开时覆盖后退按钮

转载 作者:行者123 更新时间:2023-12-02 10:42:57 26 4
gpt4 key购买 nike

我想要实现的是,当用户在 MENU 可见时单击后退按钮时,menuActualMENU 的状态从可见变为不可见。如果 MENU 未打开并且用户单击返回,则会显示一个 Toast,并显示“再次按下即可退出”,并且如果您单击2 秒内返回,应用程序将关闭。

我拥有的代码:

@Override
public void finish() {

if (MENU.getVisibility() == View.VISIBLE){
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);

}else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
moveTaskToBack(true);

return;
}else {

this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
}`

我已声明 boolean doubleBackToExitPressedOnce = false;

应用程序甚至显示 Toast“再次按退出”,但如果再次单击返回,应用程序会说“AppName isn' t 响应”

努力找出原因,这是漫长的一天。

谢谢!

最佳答案

onBackPressed中这样做:

private boolean doubleBackToExitPressedOnce = false;
private Handler handler;
private Runnable runnable;

@Override
public void onBackPressed() {
if (MENU.getVisibility() == View.VISIBLE) {
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);
return;
}

if (!doubleBackToExitPressedOnce) {
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();

handler = new Handler();
handler.postDelayed(runnable = new Runnable() {

@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
return;
}

// Removes the callBack
handler.removeCallbacks(runnable);

// Replace this next line with finishAffinity() if you want to close the app.
super.onBackPressed();
}

关于java - 菜单打开时覆盖后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52803270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com