Android:Dialog中隐藏键盘的注意事项
场景:弹出一个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中隐藏键盘的注意事项的更多相关文章
- 【ABAP系列】SAP ABAP 如何控制Dialog中的键盘(回车)功能
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 如何控制Dia ...
- 彻底搞定Android开发中软键盘的常见问题
软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog. InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参 ...
- 浅析Android Dialog中setContentView()方法
2017-05-15 概述 Dialog在Android中是一个很优秀的工具.在使用Dialog时,我们一般都会自定义要显示的内容布局.Dialog自带了三个方法来支持自定义内容布局. public ...
- Android WebView中软键盘会遮挡输入框相关问题
要想实现这样的软键盘出现的时候会自己主动把输入框的布局顶上去的效果,须要设置输入法的属性,有下面两种设置方式: 一.在java代码中设置例如以下: getWindow().setSo ...
- [转]jquerUI Dialog中隐藏标题栏的关闭"X"按钮
本文转自:http://blog.chinaunix.net/uid-144593-id-2804206.html 方法1. 在CSS文件中添加如下样式既可 .ui-dialog-titlebar-c ...
- app测试中隐藏键盘
1.参考连接 https://www.cnblogs.com/raindrop2007/articles/7849905.html 2.在项目中的使用 2.1 设置手机上的“语言输入法”,选择appi ...
- 隐藏软键盘(解决自定义Dialog中无法隐藏的问题)
/** * Dialog中隐藏软键盘不管用 * @param activity */ public static void HideSoftKeyBoard(Activity activity){ t ...
- Android Dialog 创建上下文菜单
Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...
- Spinner在Dialog中的使用效果
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/91 背景: 记得很久以前,碰到一个需求场景,需要在Andr ...
随机推荐
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...
- GDB高级用法
http://blog.csdn.net/wwwsq/article/details/7086151
- java hashcode()和equal()方法比较
通常equals,toString,hashCode,在应用中都会被复写,建立具体对象的特有的内容. 之所以有hashCode方法,是因为在批量的对象比较中,hashCode要比equals来得快,很 ...
- Extjs GridPanel 监听事件 行选中背景
Extjs设置GridPanel选中行背景色和选中单元格背景色 var view = grid.getView(); view.getRow(index).style.backgroundColor ...
- [翻译] EnterTheMatrix
Enter The Matrix https://github.com/mpospese/EnterTheMatrix The sample application to accompany my c ...
- Android之使用picker打开相应的app
Android之使用picker打开相应的app,如果是music则可以选择是否使用相应打开的app进行播放. 在Manifest中设置,则可在选择音频文件的时候使用配置了以下的app打开 <i ...
- Lombok的安装及入门
lombok 的官方网址:http://projectlombok.org/ lombok 其实到这里我就介绍完了,开个玩笑,其实官网上有 lombok 三分四十九秒的视频讲解,里面讲的也很清楚了,而 ...
- [Android Pro] AtomicInteger的用法
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...
- 深度学习研究组Deep Learning Research Groups
Deep Learning Research Groups Some labs and research groups that are actively working on deep learni ...
- Linked List Cycle leetcode java (链表检测环)
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...