按两次按钮后完成活动?

时间:2013-12-31 06:44:38

标签: android android-layout android-intent

我在这里创建一个应用程序我需要在用户两次按下后退按钮时完成活动。这是我尝试的代码

 @Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}

也尝试了这个

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
    finish();
}
return super.onKeyDown(keyCode, event);
}

这有助于我按下后退按钮完成活动。

请我提出你的建议。提前致谢

7 个答案:

答案 0 :(得分:14)

好的......这是更长,但有效的方式这样做......

1)让您的班级在全球范围内变得像...

private boolean backPressedToExitOnce = false;
private Toast toast = null;

2)然后实现onBackPressed这样的活动...

@Override
public void onBackPressed() {
    if (backPressedToExitOnce) {
        super.onBackPressed();
    } else {
        this.backPressedToExitOnce = true;
        showToast("Press again to exit");
        new Handler().postDelayed(new Runnable() {

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

3)使用此技巧有效处理此吐司...

/**
 * Created to make sure that you toast doesn't show miltiple times, if user pressed back
 * button more than once. 
 * @param message Message to show on toast.
 */
private void showToast(String message) {
    if (this.toast == null) {
        // Create toast if found null, it would he the case of first call only
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else if (this.toast.getView() == null) {
        // Toast not showing, so create new one
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else {
        // Updating toast message is showing
        this.toast.setText(message);
    }

    // Showing toast finally
    this.toast.show();
}

4)并在活动结束时使用此技巧隐藏吐司......

/**
 * Kill the toast if showing. Supposed to call from onPause() of activity.
 * So that toast also get removed as activity goes to background, to improve
 * better app experiance for user
 */
private void killToast() {
    if (this.toast != null) {
        this.toast.cancel();
    }
}

5)实现你这样的onPause(),当活动进入背景时杀死吐司

@Override
protected void onPause() {
    killToast();
    super.onPause();
}

希望这会有所帮助...... :)

答案 1 :(得分:1)

使用布尔值检查按钮是否按了两次

boolean isFinsihActivity = false;

@Override
public void onBackPressed() {
    if (isFinsihActivity) {
        super.onBackPressed();
    }
    isFinsihActivity = true;
}

答案 2 :(得分:1)

我认为这是正确的方法是等待第二次回来

private boolean _doubleBackToExitPressedOnce    = false;

@Override
public void onBackPressed() {

Log.i(TAG, "onBackPressed--");
if (_doubleBackToExitPressedOnce) {
    super.onBackPressed();
    return;
}
this._doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {

        _doubleBackToExitPressedOnce = false;
    }
}, 2000);
}

这对我有用......来自here

的回答

答案 3 :(得分:0)

Activity

中维护一个全局变量
private int count;

在您的活动中覆盖onBackPressed

@Override
public void onBackPressed() {
    if (count == 1) {
        super.onBackPressed();
    }
    count++;
}

答案 4 :(得分:0)

我要做的是使用时间戳进行第一次后退点击,如果它在时间限制内再次发生,我会完成。

long mtimeOfFirstClick
long TIME_LIMIT = 0.5 DateUtils.SECONDS_IN_MILLIS;

public void onBackPressed() {
    if(System.currentTimeMillis() - mtimeOfFirstClick <= TIME_LIMIT) {
        finish();
    } else {
        mtimeOfFirstClick = System.currentTimeMillis();
    }
}

答案 5 :(得分:0)

尝试这样初始化静态int变量为0并在onBackPressed()内增加变量并检查它的值为2;

public class MainActivity extends Activity {
static int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onBackPressed(){
    i++;
    if(i==2){

        finish();

    }

}

答案 6 :(得分:0)

做到这样的事情:

int counter=0;

    @Override
    public void onBackPressed() {
            super.onBackPressed();
            counter+=1;
            if(counter==2){
            this.finish();}
    }