向下滑动时隐藏android键盘
如何在向下滑动时隐藏android键盘?我想,若键盘是打开的,若用户在屏幕上向下滑动,那个么键盘应该隐藏起来。我如何做到这一点?您需要在视图中实现onTouchListener,然后在向下滚动事件中使键盘像这样隐藏向下滑动时隐藏android键盘,android,xamarin,xamarin.forms,Android,Xamarin,Xamarin.forms,如何在向下滑动时隐藏android键盘?我想,若键盘是打开的,若用户在屏幕上向下滑动,那个么键盘应该隐藏起来。我如何做到这一点?您需要在视图中实现onTouchListener,然后在向下滚动事件中使键盘像这样隐藏 /** * Hide Keyboard * * @param activity */ public static void hideKeyboard(Activity activity) { // Check if no
/**
* 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:
用法:
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();
}
});
您可以使用
SimpleOnGestureListener()
并覆盖它的onFling()
方法来检测刷卡。对于向下滑动,您可以将MotionEvent
的Y增量与灵敏度进行比较
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();
}
}
}
如何从调用HideKeyboard方法的位置从上到下获取滑动事件?我试过使用OnTouchEvent,但没有working@anand我编辑了我的答案,请检查并让我知道谢谢你的回答。现在它开始工作了。在xamarin中,我们有DispatchTouchEvent,我们调用它来获取touch事件。现在,每当用户刷卡时,键盘都会关闭。@A很高兴它能帮助您。请接受它作为答案并投票表决。谢谢
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();
}
}
}