向下滑动隐藏Android键盘

时间:2016-09-23 05:46:42

标签: android xamarin xamarin.forms

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

2 个答案:

答案 0 :(得分:2)

您需要在视图中实现 onTouchListener ,然后向下滚动事件让键盘隐藏起来

import React, { Platform } from 'react-native';
import _ from 'underscore';

module.exports = function (scene) {
    var componentMap = {
        'HomePage': {
            title: 'HomePage',
            id: 'HomePage'
        },
        'ProfilePage': {
            title: 'ProfilePage',
            id: 'ProfilePage'
        }
    }

    return componentMap[scene];
}

OnSwipeTouchListener.java

/**
     * 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){

        }
    }

用法:

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() {
    }
}

答案 1 :(得分: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();
       }
      }
     }