2016-09-23 90 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(); 
     } 
     } 
    }