Android Click EditText Anywhere outside of the text box to hide the keyboard solution

Source: Internet
Author: User

1, implement method one: By setting the Click event to the parent layout file of the current interface (equivalent to setting a click event for the entire activity), the keyboard is hidden in the event

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"      android:id= "@+id/traceroute_ Rootview "      android:layout_width=" fill_parent "      android:layout_height=" Fill_parent "      android: background= "@color/white"      android:clickable= "true"      android:gravity= "Center_horizontal"      android:o rientation= "Vertical" >    

Plus ID and clickable=true

Then, in OnCreate, add the listener for the onclick event:

@Override public  void OnClick (View v) {      switch (V.getid ()) {      R.id.traceroute_rootview:           Inputmethodmanager IMM = (inputmethodmanager)           getsystemservice (context.input_method_service);           Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);          break;      }    }  

  

This will perfectly solve the hidden effect outside the input box, which can be used if the layout is not particularly complex or if there are few other touch events.

2, realize the idea of two: by Dispatchtouchevent each Action_down event to dynamically determine the non-edittext itself region of the Click event, and then block in the event.

@Override Public  Boolean dispatchtouchevent (motionevent ev) {      if (ev.getaction () = = Motionevent.action_down) {          View v = getcurrentfocus ();          if (Isshouldhideinput (v, Ev)) {                Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (context.input_ Method_service);              if (IMM! = null) {                  Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);}          }          return super.dispatchtouchevent (EV);      }      necessary, otherwise all components will not have touchevent      if (GetWindow (). superdispatchtouchevent (EV)) {          return true;      }      return ontouchevent (EV);  }  

  

Isshoudhideinput (View v,motionevent e) Method:

Public  Boolean isshouldhideinput (View V, motionevent event) {      if (v = null && (v instanceof EditText)) { C2/>int[] Lefttop = {0, 0};          Gets the current location position of the input box          V.getlocationinwindow (lefttop);          int left = lefttop[0];          int top = lefttop[1];          int bottom = top + v.getheight ();          int right = left + v.getwidth ();          if (Event.getx () > Left && event.getx () < right                  && event.gety () > Top && event.gety ( < bottom) {              //Click on the Input box area, leave the event of click EditText              return false;}          else {              return true;          }      }      return false;  }  

This approach is cumbersome to implement, and the solution is similar to the event distribution mechanism in iOS, which is clearer for handling hidden events, distributed through layers of events, and then judged if the area needs to be masked.

Android Click EditText Anywhere outside of the text box to hide the keyboard solution

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.