Perfect solution Click on the blank space, hide the soft keyboard

Source: Internet
Author: User
Tags gety

When you do one-on-one chat, you should do something like that, you can hide the soft keyboard when you click on the blank and non-edittext.

Search on the Internet when the search for a reliable approach, the link is as follows: http://www.cnblogs.com/coding-way/archive/2012/07/10/2585511.html

The code is as follows:

 Public classHomeactivityextendsActivity {... @Override Public Booleandispatchtouchevent (motionevent ev) {if(ev.getaction () = =Motionevent.action_down) {            //get the currently focused view, typically the EditText (special case is the trajectory or the entity case will move the focus)View v =Getcurrentfocus (); if(Isshouldhideinput (v, Ev)) {hidesoftinput (V.getwindowtoken ()); }        }        return Super. dispatchtouchevent (EV); }    /*** To determine whether to hide the keyboard according to the coordinates of the edittext and the coordinates of the user click, because there is no need to hide when the user clicks EditText *@paramv *@paramEvent *@return     */    Private BooleanIsshouldhideinput (View V, motionevent event) {if(V! =NULL&& (vinstanceofEditText)) {            int[] L = {0, 0 };            V.getlocationinwindow (l); intleft = l[0], top = l[1], bottom = top + v.getheight (), right = Left+v.getwidth (); if(Event.getx () > Left && event.getx () < Right&& event.gety () > Top && event.gety () <bottom) {                //Click on the EditText event and ignore it.                 return false; } Else {                return true; }        }        //If the focus is not edittext then ignore, this occurs when the view has just been drawn, the first focus is not on the EditView, and the user uses the trackball to select other focus        return false; }    /*** A variety of hidden software disk methods of one of the * *@paramtoken*/    Private voidHidesoftinput (IBinder token) {if(Token! =NULL) {Inputmethodmanager im=(Inputmethodmanager) Getsystemservice (Context.input_method_service);        Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_not_always); }    }  ......}

Instead of using the EVENT.GETX () function in Isshouldhideinput, you should use the EVENT.GETRAWX () function

 Public Final float  1 This is the containing window and views.

This is the point relative to the upper-left corner of the screen

 Public Final float  1getX (int for the first pointer index (could be a arbitrary pointer identifier).

This is the coordinate of the touch point relative to the widget

If you are still not sure, you can see the following link:

Http://www.360doc.com/content/13/0815/20/7179579_307418542.shtml, this is a more detailed talk.

This article was reproduced in: http://blog.csdn.net/sanjinxiong/article/details/9989517

Actual use:

@Override Public Booleandispatchtouchevent (motionevent ev) {if(ev.getaction () = =Motionevent.action_down) {            //get the currently focused view, typically the EditText (special case is the trajectory or the entity case will move the focus)//View v = getcurrentfocus ();            if(Isshouldhideinput (EV)) {dismisskeyboard (); }        }        return Super. dispatchtouchevent (EV); }    /*** To determine whether to hide the keyboard according to the coordinates of the edittext and the coordinates of the user click, because there is no need to hide when the user clicks EditText *@paramv *@paramEvent *@return     */    Private BooleanIsshouldhideinput (Motionevent event) {if(Et_txt! =NULL) {            int[] E = {0, 0};            Et_txt.getlocationinwindow (E); intLeftedit = E[0], Topedit = e[1], Bottomedit = Topedit + et_txt.getheight (), Rightedit =Leftedit+et_txt.getwidth (); if(Event.getx () > Leftedit && event.getx () < Rightedit && event.gety () >Topedit&& event.gety () <Bottomedit) {                //Click on the EditText event and ignore it.                 return false; } Else {                return true; }        }             //If the focus is not edittext then ignore, this occurs when the view has just been drawn, the first focus is not on the EditView, and the user uses the trackball to select other focus            return false; }
Et_txt: Text input box for the current page

Perfect solution Click on the blank space, hide 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.