Android - 使用吐司确认应用退出

问题描述 投票:9回答:6

我是Android开发的新手,我想要它,所以当用户按下主活动上的后退按钮时,会出现一个Toast消息,其中“再次按下后退按钮确认退出”消息。我该怎么做?这是我到目前为止:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Toast s = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
    s.show();
    wait();

    public boolean onBackPressed() {
        finish();    
    }
}
android exit toast back confirm
6个回答
19
投票

我只是节省了背压的时间,然后将最新印刷机的时间与新印刷机进行比较。

long lastPress;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if(currentTime - lastPress > 5000){
        Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG).show();
        lastPress = currentTime;
    }else{
        super.onBackPressed();
    }
}

当应用程序确认背压时,你也可以解雇吐司(cred @ToolmakerSteve):

long lastPress;
Toast backpressToast;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if(currentTime - lastPress > 5000){
        backpressToast = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
        backpressToast.show();
        lastPress = currentTime;
    } else {
        if (backpressToast != null) backpressToast.cancel();
        super.onBackPressed();
    }
}

2
投票
private long mLastPress = 0;
const int TOAST_DURATION = 5000;
Toast onBackPressedToast;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if (currentTime - mLastPress > TOAST_DURATION) {
        onBackPressedToast = Toast.makeText(this, R.string.press_once_again_to_exit, Toast.LENGTH_SHORT);
        onBackPressedToast.show();
        mLastPress = currentTime;
    } else {
        if (onBackPressedToast != null) {
            onBackPressedToast.cancel();  //Difference with previous answer. Prevent continuing showing toast after application exit.
            onBackPressedToast = null;
        }
        super.onBackPressed();
    }
}

1
投票

在我的情况下完美地工作

private static long back_pressed;

@Override
public void onBackPressed()
{
        if (back_pressed + 2000 > System.currentTimeMillis()) 
               super.onBackPressed();
        else 
             Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
        back_pressed = System.currentTimeMillis();
}

0
投票

在较新的版本中,您可以使用快餐栏而不是吐司。

import android.support.design.widget.Snackbar;
...
Snackbar.make(content, "Click BACK again to exit", Snackbar.LENGTH_SHORT).setAction("Action", null).show();
...

0
投票

在不得不多次执行相同的行为之后,决定去建立一个同样的库:DoubleBackPress Android Library。它提供了许多易于使用的模板和双重背压行为,没有任何麻烦。

做就是了 :

// set the ToastDisplay to be shown on FirstBackPress
FirstBackPressAction firstBackPressAction = new ToastDisplay().standard(this);

// set the Action on DoubleBackPress
DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() {
    @Override
    public void actionCall() {
        finish();
    }
};

// setup DoubleBackPress behaviour
DoubleBackPress doubleBackPress = new DoubleBackPress()
        .withDoublePressDuration(3000)
        .withFirstBackPressAction(firstBackPressAction)
        .withDoubleBackPressAction(doubleBackPressAction);

最后,使用DoubleBackPress行为覆盖onBackPress以进行反压。

@Override
public void onBackPressed() {
    doubleBackPress.onBackPressed();
}

Example GIF for similar behaviour


0
投票

使用Toast的最佳和简单的解决方案

在Java中

private Toast exitToast;

@Override
public void onBackPressed() {
    if (exitToast == null || exitToast.getView() == null || exitToast.getView().getWindowToken() == null) {
        exitToast = Toast.makeText(this, "Press again to exit", Toast.LENGTH_LONG);
        exitToast.show();
    } else {
        exitToast.cancel();
        finish();
    }
}

在科特林

private var exitToast: Toast? = null

override fun onBackPressed() {
    if (exitToast == null || exitToast!!.view == null || exitToast!!.view.windowToken == null) {
        exitToast = Toast.makeText(this, "Press again to exit", Toast.LENGTH_LONG)
        exitToast!!.show()
    } else {
        exitToast!!.cancel()
        finish()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.