场景:弹出一个Dialog。里面有一个EditText。用来输入内容。由于输入时。须要弹出键盘。所以当Dialog消失时。键盘要一起隐藏。

如今我们做一个自己定义的Dialog

MyDialog extends Dialog

一開始觉得这个功能非常easy实现,于是写了以下的代码

//Dialog的构造函数中写
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
hideKeyBoard();
}
});
//edContent是输入框
public void hideKeyBoard(){
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(edContent.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

执行之后,发现根本无法隐藏。看看hideSoftInputFromWindow中干了啥

    public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
ResultReceiver resultReceiver) {
checkFocus();
synchronized (mH) {
if (mServedView == null || mServedView.getWindowToken() != windowToken) {
return false;
} try {
return mService.hideSoftInput(mClient, flags, resultReceiver);
} catch (RemoteException e) {
}
return false;
}
}

跟踪进去发现參数 windowToken 是 null,并且 mServedView 也是null,所以直接返回false,无法隐藏。

也就是说,你监听Cancel或者Dismiss都是不行的。由于此时Dialog已经消失。用于输入的服务窗口已经是null了,所以你要想 隐藏键盘,就须要在Dismiss之前处理,那这个入口在哪呢?

为了当点击空白处时,能够隐藏Dialog,所以我们在构造函数中加了一句话

this.setCanceledOnTouchOutside(true);

所以当我们点击空白区域时。会触发Dialog的onTouchEvent

    public boolean onTouchEvent(MotionEvent event) {
if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
cancel();
return true;
} return false;
}

这里会调用基类Window的shouldCloseOnTouch方法,来推断能否够关闭,这里我们看到假设满足,就直接cancel()了,

    public void cancel() {
if (!mCanceled && mCancelMessage != null) {
mCanceled = true;
// Obtain a new message so this dialog can be re-used
Message.obtain(mCancelMessage).sendToTarget();
}
dismiss();
}

这里面就会dismiss掉Dialog,所以我们发现,在dismiss前。我们根本无法干预,真是个悲剧。

所以我们仅仅能重载onTouchEvent方法。并且自己推断能否够关闭(也就是把以下代码迁移到你的代码中!

    public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN
&& isOutOfBounds(context, event) && peekDecorView() != null) {
return true;
}
return false;
} private boolean isOutOfBounds(Context context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}

自己代码中这样

  @Override
public boolean onTouchEvent(MotionEvent event) {
if (isShowing() && shouldCloseOnTouch(getContext(),event)){
hideKeyBoard();
}
return super.onTouchEvent(event);
}
public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
&& isOutOfBounds(context, event) && getWindow().peekDecorView() != null) {
return true;
}
return false;
}
private boolean isOutOfBounds(Context context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = getWindow().getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}

真是有点无奈。眼下想到的方法就是这样。哪位有更好的方法,欢迎提供!

Android:Dialog中隐藏键盘的注意事项的更多相关文章

  1. 【ABAP系列】SAP ABAP 如何控制Dialog中的键盘(回车)功能

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 如何控制Dia ...

  2. 彻底搞定Android开发中软键盘的常见问题

    软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog.        InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参 ...

  3. 浅析Android Dialog中setContentView()方法

    2017-05-15 概述 Dialog在Android中是一个很优秀的工具.在使用Dialog时,我们一般都会自定义要显示的内容布局.Dialog自带了三个方法来支持自定义内容布局. public ...

  4. Android WebView中软键盘会遮挡输入框相关问题

    要想实现这样的软键盘出现的时候会自己主动把输入框的布局顶上去的效果,须要设置输入法的属性,有下面两种设置方式:     一.在java代码中设置例如以下:      getWindow().setSo ...

  5. [转]jquerUI Dialog中隐藏标题栏的关闭"X"按钮

    本文转自:http://blog.chinaunix.net/uid-144593-id-2804206.html 方法1. 在CSS文件中添加如下样式既可 .ui-dialog-titlebar-c ...

  6. app测试中隐藏键盘

    1.参考连接 https://www.cnblogs.com/raindrop2007/articles/7849905.html 2.在项目中的使用 2.1 设置手机上的“语言输入法”,选择appi ...

  7. 隐藏软键盘(解决自定义Dialog中无法隐藏的问题)

    /** * Dialog中隐藏软键盘不管用 * @param activity */ public static void HideSoftKeyBoard(Activity activity){ t ...

  8. Android Dialog 创建上下文菜单

    Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...

  9. Spinner在Dialog中的使用效果

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/91 背景: 记得很久以前,碰到一个需求场景,需要在Andr ...

随机推荐

  1. AES advanced encryption standard 2

    /* * FIPS-197 compliant AES implementation * * Copyright (C) 2006-2007 Christophe Devine * * Redistr ...

  2. java基础学习总结——static关键字

    一.static关键字

  3. 点集转线python最优代码

    import arcpy import os import types def convertPoints(): arcpy.env.overwriteOutput = True inPts = ar ...

  4. 初次使用SQL调优建议工具--SQL Tuning Advisor

    在10g中,Oracle推出了自己的SQL优化辅助工具: SQL优化器(SQL Tuning Advisor :STA),它是新的DBMS_SQLTUNE包. 使用STA一定要保证优化器是CBO模式下 ...

  5. C\C++各路高手以及操作系统专家请进来杀死这个进程

    通常情况下编写一个程序,能够点击关闭button正常结束程序,也能够使用任务管理器结束任务,还能够使用taskkill等命令杀死进程,实在都不行也能够直接重新启动计算机. 可是,这些方法真的都管用吗? ...

  6. 使用 DES 算法对数据加密

    DES算法 ☆提供高质量的数据保护,防止数据未经授权的泄露和未被察觉的修改 ☆具有相当高的复杂性,使得破译的开销超过可能获得的利益,同时又要便于理解和掌握 ☆DES密码体制的安全性应该不依赖于算法的保 ...

  7. 服务 Service 清单文件中可设置的属性

    PS:对于一个Service,在没有在AndroidManifest.xml中声明的情况下使用时,不会像Activity那样直接崩溃并提示找不到Activity. 对于显式Intent启动的Servi ...

  8. Golang 中使用多维 map

    http://tnt.wicast.tk/2015/11/02/golang-multiple-dimension-map/ Golang 的 XML/JSON 解析库乍看使用起来很方便,只要构造一样 ...

  9. Linq-语句之存储过程

    存储过程 在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在 ...

  10. 关于帝国CMS迁移到新服务器上出现问题的处理办法

    在帝国CMS项目整体迁移过程中,或多或少总会出点幺蛾子,以下就常见的注意事项整理一下: 一.修改 e/config/config.php中的数据库相关配置 二.让项目文件位置具有读写权限 三.设置ph ...