2016-09-23 93 views
-5

如何隐藏安卓键盘向下滑动?我想,如果键盘打开,并且用户在屏幕上向下滑动,则键盘应该隐藏。我如何做到这一点?向下滑动隐藏安卓键盘

回答

2

需要实现onTouchListener到您的视图,然后向下滚动事件使键盘隐藏这样

/** 
    * Hide Keyboard 
    * 
    * @param activity 
    */ 
    public static void hideKeyboard(Activity activity) { 
     // Check if no view has focus: 
     try { 
      View view = activity.getCurrentFocus(); 
      if (view != null) { 
       InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     }catch (Exception e){ 

     } 
    } 

OnSwipeTouchListener.java

import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector; 

    public OnSwipeTouchListener (Context ctx){ 
     gestureDetector = new GestureDetector(ctx, new GestureListener()); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 
    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
        result = true; 
       } 
       else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
        result = true; 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 

    public void onSwipeRight() { 
    } 

    public void onSwipeLeft() { 
    } 

    public void onSwipeTop() { 
    } 

    public void onSwipeBottom() { 
    } 
} 

用法:

imageView.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) { 
    public void onSwipeTop() { 
     Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeRight() { 
     Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeLeft() { 
     Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeBottom() { 
     Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
    } 

}); 
+0

如何从上到下得到滑动事件,我将从哪里调用HideKeyboard方法?我尝试过使用OnTouchEvent,但它不起作用 – anand

+0

@anand我编辑了我的答案,请检查并让我知道 –

+0

感谢您的回答。现在它的工作。在Xamarin中,我们有DispatchTouchEvent,我们称之为获取触摸事件。当用户滑动时,现在键盘关闭。 – anand

0

您可以使用SimpleOnGestureListener()并覆盖它的onFling()方法来检测滑动。对于向下滑动,您将Y的MotionEvent的增量与灵敏度进行比较。

public static void hideKeyboard(Activity activity) { 
     // Check if no view has focus: 
     try { 
      View view = activity.getCurrentFocus(); 
      if (view != null) { 
       InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     }catch (Exception e){ 

     } 
    } 

    SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 

     float sensitivity = 50; //suit yourself 

     // TODO Auto-generated method stub 

     if((e2.getY() - e1.getY()) > sensitivity){ 
     //swiped down, now do whatever you want. 
     hideKeyboard(); 
     } 
     } 
    }