【问题标题】:Hide android keyboard on swipe down向下滑动时隐藏 android 键盘
【发布时间】:2017-01-31 21:07:09
【问题描述】:

如何在向下滑动时隐藏 android 键盘?我希望如果键盘打开并且用户在屏幕上向下滑动,那么键盘应该隐藏。我该怎么做?

【问题讨论】:

    标签: android xamarin xamarin.forms


    【解决方案1】:

    您需要在视图中实现 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();
        }
    
    });
    

    【讨论】:

    • 如何从调用 HideKeyboard 方法的位置从上到下获取滑动事件?我尝试使用 OnTouchEvent 但它不起作用
    • @anand 我编辑了我的答案,请检查并告诉我
    • 感谢您的回答。现在它的工作。在 xamarin 中,我们调用了 DispatchTouchEvent 来获取触摸事件。现在,只要用户滑动,键盘就会关闭。
    • @anand 很高兴它帮助你。请接受它作为答案并投票。谢谢
    【解决方案2】:

    您可以使用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();
           }
          }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2021-07-13
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多