2012-01-02 55 views
26

当用户点击Edittext以外的任何地方时,我需要隐藏android中的softkeypad。 iphone有很多帮助,但不适用于android。我想这个代码,但它不是事先工作:(隐藏键盘当用户点击屏幕上的任何其他地方android

final RelativeLayout base = (RelativeLayout) findViewById(R.id.RelativeLayout1); 

    findViewById(R.id.base).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(base.getWindowToken(), 0); 

     } 
    }); 

谢谢!

+0

可以请帮问题清楚了吗? – 2012-01-02 04:59:52

+0

我编辑问题。希望它更容易理解。 – Chrishan 2012-01-02 05:10:22

+0

也许是一个重复的问题[这](http://stackoverflow.com/questions/4005728/hide-default-keyboard-on-click-in-android),检查出来 – manuzhang 2012-01-02 05:10:48

回答

51

可以使用onTouchEvent()隐藏Softkeyboard

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     InputMethodManager imm = (InputMethodManager)getSystemService(Context. 
                 INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
     return true; 
    } 

虽然此解决方案有效,但最好我建议使用低于answer,因为它提供了最好的解决方案,关闭键盘接触其他地方然后EditText。

+1

+1是的,我想是的,希望它不考虑键盘按键作为活动触摸事件..但是,我认为这是方式。 – MKJParekh 2012-01-02 05:42:30

+0

如果你有任何其他的观点,你可以使用它的* instance.getWindowToken()*,它会工作。 – 2012-01-02 05:44:11

+1

这不适合我。我找到了正确的[这里](http://stackoverflow.com/a/7241790/651422) – Chrishan 2012-01-02 06:15:17

13

最好的解决办法,我发现是使用如下,

覆盖dispatchTouchEvent()并尝试使用该caculates 4角落查看(在这里它的EditText)的

Rect

@Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     int x = (int) ev.getX(); 
     int y = (int) ev.getY(); 

     if (ev.getAction() == MotionEvent.ACTION_DOWN && 
      !getLocationOnScreen(etFeedback).contains(x, y)) { 
      InputMethodManager input = (InputMethodManager) 
            activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
      input.hideSoftInputFromWindow(etFeedback.getWindowToken(), 0); 
     } 

     return super.dispatchTouchEvent(ev); 
    } 

方法得到的EditText面积

protected Rect getLocationOnScreen(EditText mEditText) { 
     Rect mRect = new Rect(); 
     int[] location = new int[2]; 

     mEditText.getLocationOnScreen(location); 

     mRect.left = location[0]; 
     mRect.top = location[1]; 
     mRect.right = location[0] + mEditText.getWidth(); 
     mRect.bottom = location[1] + mEditText.getHeight(); 

     return mRect; 
    } 

通过使用上面的代码,我们可以检测到EditText的区域,我们可以检查屏幕上的触摸是否是EditText区域的一部分。如果它的一部分EditText则不做任何事情让触摸做它的工作,如果触摸不包含EditText区域,那么只需关闭软键盘。

****** EDIT ******

我才发现,如果我们不希望给任何的EditText作为输入,并希望隐藏键盘整个应用程序中,当用户触摸任何地方的另一种方法除EditText以外的其他。然后,你必须创建一个BaseActivity和写入全球规范,下面隐藏键盘,

@Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 

     boolean handleReturn = super.dispatchTouchEvent(ev); 

     View view = getCurrentFocus(); 

     int x = (int) ev.getX(); 
     int y = (int) ev.getY(); 

     if(view instanceof EditText){ 
      View innerView = getCurrentFocus(); 

      if (ev.getAction() == MotionEvent.ACTION_UP && 
            !getLocationOnScreen(innerView).contains(x, y)) { 

       InputMethodManager input = (InputMethodManager) 
            getSystemService(Context.INPUT_METHOD_SERVICE); 
       input.hideSoftInputFromWindow(getWindow().getCurrentFocus() 
                   .getWindowToken(), 0); 
      } 
     } 

     return handleReturn; 
    } 
+0

你能解释一下代码吗? :) – AnhTriet 2015-06-24 16:44:04

+0

@ Forte_201092是不是已经解释过了? ;)好的,进一步解释:) – 2015-06-26 05:48:15

+0

我明白了。非常感谢:D – AnhTriet 2015-06-26 08:22:08

21

嗯,我已经做了这种方式:

将代码添加在你活动

这将工作片段无需片段添加此代码。

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    View view = getCurrentFocus(); 
    if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) { 
     int scrcoords[] = new int[2]; 
     view.getLocationOnScreen(scrcoords); 
     float x = ev.getRawX() + view.getLeft() - scrcoords[0]; 
     float y = ev.getRawY() + view.getTop() - scrcoords[1]; 
     if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom()) 
     ((InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this.getWindow().getDecorView().getApplicationWindowToken()), 0); 
    } 
    return super.dispatchTouchEvent(ev); 
} 

希望这会对你有帮助。

+3

方法完美谢谢。 – SalutonMondo 2016-06-22 01:30:41

+3

适合我。谢谢! – 2016-06-28 18:08:24

+2

也适用于自定义对话框。 – 2017-01-27 10:21:34

0

我试着隐藏键盘。您需要在布局文件中传递该方法。

public void setupUI(View view) { 
    // Set up touch listener for non-text box views to hide keyboard. 
    if (!(view instanceof EditText)) { 
     view.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       hideSoftKeyboard(LOGSignUpActivity.this); 
       return false; 
      } 
     }); 
    } 
    //If a layout container, iterate over children and seed recursion. 
    if (view instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
      View innerView = ((ViewGroup) view).getChildAt(i); 
      setupUI(innerView); 
     } 
    } 
} 
0

调用此方法在您的MainAcitivity.class

public static void hideKeyboardwithoutPopulate(BaseActivity activity) { 
    InputMethodManager inputMethodManager = 
      (InputMethodManager) activity.getSystemService(
        Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(
      activity.getCurrentFocus().getWindowToken(), 0); 
} 
0

对于那些谁正在寻找这个Xamarin的代码,在这里你去:

public override bool DispatchTouchEvent(MotionEvent ev) 
    { 
     try 
     { 
      View view = CurrentFocus; 
      if (view != null && (ev.Action == MotionEventActions.Up || ev.Action == MotionEventActions.Move) && view is EditText && !view.Class.Name.StartsWith("android.webkit.")) 
      { 
       int[] Touch = new int[2]; 
       view.GetLocationOnScreen(Touch); 
       float x = ev.RawX + view.Left - Touch[0]; 
       float y = ev.RawY + view.Top - Touch[1]; 
       if (x < view.Left || x > view.Right || y < view.Top || y > view.Bottom) 
        ((InputMethodManager)GetSystemService(InputMethodService)).HideSoftInputFromWindow((Window.DecorView.ApplicationWindowToken), 0); 
      } 
     } 
     catch (System.Exception ex) 
     { 

     } 

     return base.DispatchTouchEvent(ev); 
    } 
相关问题