Android BACK键单击两次

时间:2012-12-06 17:51:35

标签: android android-button

我想在点击一次BACK键时显示一些内容,并在点击两次时关闭应用程序。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (/* BACK_KEY_was_clicked_once_again */) {
            finish();
        }
        else
        {
            text.setText(log);
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

抱歉,我没有解释清楚

主要(比如说)我的应用程序的视图是:text.setText(log),但是当我点击菜单中的“info”按钮时:text.setText(info)。当设置“info”时我想要BACK按钮:text.setText(log),当设置“log”时我想要Back按钮关闭我的应用程序。

现在我意识到我真正想做的事情,并将代码更改为:

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
  if (text.getText() == log) {
    finish();
  }
  else
  {
    text.setText(log);
  }
  return true;
}
return super.onKeyDown(keyCode, event);
}

6 个答案:

答案 0 :(得分:3)

使用Class级别字段作为标志来检查后退按钮是否按下两次或不是:

 public static boolean backtwice=false;  //this is flag

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (backtwice) {
           backtwice=false;  //reset here
            finish();
        }
        else
        {
            text.setText(log);
           backtwice=true;  //set true if first time back pressed
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:3)

不确定你要做什么,但是

private boolean backPressed = false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (backPressed == true) {
            finish();
        }
        else
        {
            backPressed = true;
            text.setText(log);
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

可能会解决您的问题......?

答案 2 :(得分:1)

只需添加活动:

private boolean doubleBackPressedOnce = false;

@Override
protected void onResume() {
    super.onResume();
    // .... other stuff in my onResume ....
    this.doubleBackPressedOnce = false;
}

@Override
public void onBackPressed() {
    if (doubleBackPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.doubleBackPressedOnce = true;
    Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}

答案 3 :(得分:0)

维护一个静态布尔标志

默认情况下将标志初始化为false。

现在改变这样的代码

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
      if(flag == false)//indicates back button is pressed 1st time {                
           flag=true;
           //do operation for first back button press
           text.setText(log);              
       }
    else //indicating pressed 2nd time
   {
        finish();
    }
    return true;
}
return super.onKeyDown(keyCode, event);
}

答案 4 :(得分:0)

private boolean twiBackToExitPressedOnce = false;

@Override
protected void onResume() {
    super.onResume();
    // .... other stuff in my onResume ....
    this.twiBackToExitPressedOnce= false;
}

@Override
public void onBackPressed() {
    if (twiBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.twiBackToExitPressedOnce= true;
    Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}

答案 5 :(得分:0)

  

单击BACK键一次显示内容,单击两次时关闭应用

这可以使用DoubleBackPress Android Library轻松处理,完全针对这种情况创建。它提供了易于使用的API,没有任何处理麻烦。

请执行以下操作:

FirstBackPressAction firstBackPressAction = new FirstBackPressAction() {
    @Override
    public void actionCall() {
        // TODO : whatever needs to be shown on the First Back key press 
    }
};

DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() {
    @Override
    public void actionCall() {
        // TODO : Exit app code
    }
};

DoubleBackPress doubleBackPress = new DoubleBackPress()
        .withDoublePressDuration(3000)   // msec to wait for second back press
        .withFirstBackPressAction(firstBackPressAction)
        .withDoubleBackPressAction(doubleBackPressAction);

最后,使用所需的行为覆盖您的背压行为。

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

An example GIF of similar behaviour