When I am writing a comment recently, I want the following requirement: click the comment button to pop up the soft keyboard and dialog box, and then press the phone return button, the dialog box and the soft keyboard will disappear at the same time, instead of the software disk disappearing first and then the dialog box will disappear after pressing the back button. Let’s start with a GIF… This recording is a bit delayed, the real experience is similar to toutiao, MAC GIF does not move… Oh, let’s publish it on Windows.)

Analysis of message processing mechanism of Android application Keyboard

Dispatch a key event before it is processed by any input method associated with the view hierarchy. This can be used to intercept key events in special situations before the IME consumes them; a typical example would be handling the BACK key to update the application’s UI instead of allowing the IME to see it and close itself.

The people will be fuxxed, which means that The method responds to The event before The soft keyboard responds to it. This method takes precedence over the soft keyboard in responding to events when the Back key is clicked. Test it out.

 @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        Log.d("MyEditText"."dispatchKeyEventPreIme method is called");
        return super.dispatchKeyEventPreIme(event);
    }
Copy the code

We click the comment button, at this time the pop-up dialog box, we click back, if the above method is followed, then we can implement our logic.


    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if(mOnCancelDialogImp ! = null) { mOnCancelDialogImp.onCancelDialog(); } Log.d("MyEditText"."dispatchKeyEventPreIme method is called");
        return super.dispatchKeyEventPreIme(event);
    }

    private OnCancelDialogImp mOnCancelDialogImp;

    public void setOnCancelDialogImp(OnCancelDialogImp onCancelDialogImp) { mOnCancelDialogImp = onCancelDialogImp; } /** * Define an interface here for hitting the back button when the input box pops up (comment) does not respond to the onKeyDown and onKeyPressed methods * but queries the API It is possible to send dispatchKeyEventPreIme within a custom view (this method responds before the soft keyboard response) * so when pressing back this method will definitely take precedence Public interface OnCancelDialogImp {void onCancelDialog(); void onCancelDialog(); }Copy the code

The interface and method have been written, how to use it? ##### first change the official EditText used in XML to our own (full path)

  <com.renren.ruolan.travelaround.widget.MyEditText
                android:id="@+id/edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Just say something."
                android:textSize="14sp"
                android:padding="5dp"
                android:gravity="top"
                android:background="@null"
                />
Copy the code

##### Secondly, the place we are using (i.e. activity or fragment or other places)

/ * * * this method will set off when press the back button. * / mEditText setOnCancelDialogImp (new MyEditText.OnCancelDialogImp() {
            @Override
            public void onCancelDialog() {// Check whether the pop-up box is emptyif(mBottomDialog ! = null) { mBottomDialog.dismiss(); MBottomDialog = null; }}});Copy the code

Ok, at this point we are ready to implement the GIF demo we started with. Is it easy to listen to.