Back button double click
I am new android developer. I want to 开发者_运维问答ask a question. Here is what I need: When the user click Back Button it counts as double click?
@Override
public void onBackPressed() {
--> what to write here?
return;
}
}
You need to check an interval between to presses and determine whether you it can be counted as a double click or not:
private static final long DOUBLE_PRESS_INTERVAL = /* some value in ns. */;
private long lastPressTime;
@Override
public void onBackPressed() {
long pressTime = System.nanoTime();
if(pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
// this is a double click event
}
lastPressTime = pressTime;
}
You should probably include the reasoning behind wanting this functionality in the question instead of a comment. It makes it a lot easier for us to point you in the right direction. There are a few ways to achieve what you want but I would not recommend the 'double back' method.
Instead, if you show the progress bar in a dialog or somewhere in the search activity, there is no activity between the search and the second activity. That way you do not need to do a double back.
Also, you could display the progress bar in the second activity until the work is done and then replace it with the actual content with another call to setContentView(View)
. Note that this would require threading though (otherwise the progress bar would never show).
Double Key Button
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();
}
精彩评论