background

Usually more common is to press the back button twice within a certain period of time to exit the application, and the first click will have the corresponding prompt, online information is more, write here, long press the back button to exit.

implementation

There are two commonly used implementation schemes:

  • rewritedispatchKeyEvent(KeyEvent event)methods
  • rewriteonKeyDown(int keyCode, KeyEvent event)methods

Solution a:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isLongPress()) {
        //do something else
        System.exit(0);
        return true;
    }
    return false;
}Copy the code

The code is very simple, if the back key is pressed and long pressed, exit.

Scheme 2:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Toast.makeText(this."Long press the back key to exit.", Toast.LENGTH_SHORT).show();
    } else if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 2) {
        //do something else
        System.exit(0);
    }
    return false;
}Copy the code

You can change The Times of event.getrepeatCount ()== in else If to change the duration of long press. If event.getrepeatCount () is 0, it indicates that a click was performed; if The Times of event. GetRepeatCount () is greater than =1, it indicates that a long press was performed. This solution is recommended because it alerts the user the first time the back key is clicked.

Afterword.

Originally prepared to sort out more information together to send out, the Nuggets just in the private release tool, here is a handwritten to see how it feels, the experience is still great. Here is my personal opinion:

Advantages:

  • Beautiful color
  • Clean interface, writing more focused, suitable for experienced hands
  • Quick response, yesMarkDownGrammatical details are well handled

Disadvantages:

  • The interface does not guide, novice will be confused
  • The color scheme of the left writing area is dark with no choice.
  • Advanced functions such as flow charts are not supported

Generally speaking, the basic functions are available and the interface is relatively clean, which is both advantage and disadvantage. Relatively speaking, it is more suitable for those who have memorized MarkDown syntax by heart.

Finally, here’s a picture of the writing interface: