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

Source: Internet
Author: User
Tags gety

Original address: Android Click anywhere outside the EditText text box to hide the keyboard solution

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

[Java]View PlainCopy
  1. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. android:id="@+id/traceroute_rootview"
  3. Android:layout_width="Fill_parent"
  4. android:layout_height="Fill_parent"
  5. android:background="@color/white"
  6. android:clickable="true"
  7. android:gravity="Center_horizontal"
  8. android:orientation="vertical" >
  9. </LinearLayout>

Plus ID and clickable=true

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

[Java]View PlainCopy
    1. Findviewbyid (R.id.traceroute_rootview). Setonclicklistener (this);

In the onclick:

[Java]View PlainCopy
  1. @Override
  2. Public void OnClick (View v) {
  3. switch (V.getid ()) {
  4. Case R.id.traceroute_rootview:
  5. Inputmethodmanager IMM = (Inputmethodmanager)
  6. Getsystemservice (Context.input_method_service);
  7. Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);
  8. Break ;
  9. }
  10. }


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.

[Java]View PlainCopy
  1. @Override
  2. Public Boolean dispatchtouchevent (motionevent ev) {
  3. if (ev.getaction () = = Motionevent.action_down) {
  4. View v = getcurrentfocus ();
  5. if (isshouldhideinput (v, Ev)) {
  6. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
  7. if (IMM! = null) {
  8. Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);
  9. }
  10. }
  11. return super.dispatchtouchevent (EV);
  12. }
  13. //necessary, otherwise none of the components will be touchevent.
  14. if (GetWindow (). superdispatchtouchevent (EV)) {
  15. return true;
  16. }
  17. return ontouchevent (EV);
  18. }


Isshoudhideinput (View v,motionevent e) Method:

[Java]View PlainCopy
  1. Public Boolean isshouldhideinput (View V, motionevent event) {
  2. if (v = null && (v instanceof EditText)) {
  3. int[] lefttop = { 0, 0};
  4. //Get the current location of the input box
  5. V.getlocationinwindow (Lefttop);
  6. int left = lefttop[0];
  7. int top = lefttop[1];
  8. int bottom = top + v.getheight ();
  9. int right = left + v.getwidth ();
  10. if (Event.getx () > Left && event.getx () < right
  11. && event.gety () > Top && event.gety () < bottom) {
  12. //Click on the Input box area to retain the Click EditText event
  13. return false;
  14. } Else {
  15. return true;
  16. }
  17. }
  18. return false;
  19. }

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 clicks anywhere outside the EditText 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.