Android Click anywhere else to close the soft keyboard

Source: Internet
Author: User

In the development of Android applications, often such a requirement, the user in the process of entering text, may not want to continue to input, by swiping or tapping other locations (except the soft keyboard and edittext anywhere), want to be able to automatically retract the keyboard, this feature may be some ROM will be implemented, But most still do not have to achieve this function, then if we want to achieve, how to solve it?

First of all, of course we have to intercept any other user touching the screen events by rewriting the activity's Boolean dispatchtouchevent (Motionevent ev), but implementing the blocking user's touch event. The code is as follows:

1     @Override 2      Public Boolean dispatchtouchevent (motionevent ev) {3         Displayutils.hideinputwhentouchotherview (This, Ev, Getexcludetouchhideinputviews ()); 4         return Super . dispatchtouchevent (EV); 5     }

The Displayutils.hideinputwhentouchotherview method is our own implementation of the code that hides the soft keyboard when necessary. The method for Hideinputwhentouchotherview is implemented as follows:

1    /**2 * Hide Soft keyboard When other view is clicked3      * @paramActivity4      * @paramEV5      * @paramExcludeviews Clicking on these views does not trigger the Hide soft keyboard action6      */7      Public Static Final voidHideinputwhentouchotherview (activity activity, motionevent EV, list<view>excludeviews) {8 9 Ten         if(ev.getaction () = =Motionevent.action_down) { One             if(Excludeviews! =NULL&&!Excludeviews.isempty ()) { A                  for(inti = 0; I < excludeviews.size (); i++){ -                     if(Istouchview (Excludeviews.get (i), Ev)) { -                         return; the                     } -                 } -             } -View v =Activity.getcurrentfocus (); +             if(Displayutils.isshouldhideinput (v, Ev)) { -Inputmethodmanager Inputmethodmanager =(Inputmethodmanager) + Activity.getsystemservice (context.input_method_service); A                 if(Inputmethodmanager! =NULL){ atInputmethodmanager.hidesoftinputfromwindow (V.getwindowtoken (), 0); -                 } -             } -  -         } -}

The excludeviews in the method parameter indicates that clicking on this view does not attempt to hide the soft keyboard, because some of the touch events of the view we do not want to hide the soft keyboard, such as the edittext background layout, the Chat input box send button, etc. (if you touch the Send button, The keyboard will be taken back to it is not very egg pain

It can be seen that the entire method of the implementation process is to determine the touch location is the view, if it is necessary to exclude the view, it is not processed directly. If you decide to click on any other non-edittext view, the soft keyboard will be hidden. The other two judgment functions are as follows:

1      Public Static Final BooleanIstouchview (view view, Motionevent event) {2         if(View = =NULL|| event = =NULL){3             return false;4         }5         int[] Lefttop = {0, 0};6 View.getlocationinwindow (lefttop);7         intleft = Lefttop[0];8         inttop = lefttop[1];9         intBottom = top +view.getheight ();Ten         intright = left +view.getwidth (); One         if(EVENT.GETRAWX () > Left && event.getrawx () < Right A&& Event.getrawy () > Top && event.getrawy () <bottom) { -             return true; -         } the         return false; -     } -  -      Public Static Final BooleanIsshouldhideinput (View V, motionevent event) { +         if(V! =NULL&& (vinstanceofEditText)) { -             return!Istouchview (V, event); +         } A         return false; at}

By reusing the Hideinputwhentouchotherview () method (even if you can do it in the base class activity), you can use the minimum code in any activity to pick up the soft keyboard at any other location. If there is anything in the code that needs correction or a better alternative, please let us know in time.

Android Click anywhere else to close the soft keyboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.