setCanceledOnTouchOutside(true)
,点击阴影处,dialog dismiss时键盘不消失的问题。
一开始觉得很简单,监听下onDimiss()
方法,在里面隐藏键盘不就行了。
但是发现大多数手机都不会隐藏(魅族x4会隐藏)。
这是为什么呢?为什么键盘不消失呢?
经过测试,发现edittext.getWindowToken()
为null。
/** * 关闭键盘 * * @param context * @param et */public static void hideKeyboard(Context context, EditText et) {InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);LogUtils.showLog("hideKeyboardimm.isActive() = "+imm.isActive()+"et.getWindowToken() = "+et.getWindowToken());if (imm.isActive()) {imm.hideSoftInputFromWindow(et.getWindowToken(), 0);}}
这是因为当ondismiss 方法执行的时候,dialog已经消失了。已经获取不到windowToken了。
目前发现有两种方式觉得这样的问题
1.在ondismiss()方法里面这样隐藏软键盘
@Overridepublic void onDismiss(DialogInterface dialog) {InputMethodManager inputMgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);}2.在dismiss之前就隐藏软键盘,因为设置setCancelOnTouchOutside(true),会响应Dialog类的onTouch方法。
public boolean onTouchEvent(MotionEvent event) {if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {cancel();return true;}return false;}/** * Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will * also call your {@link DialogInterface.OnCancelListener} (if registered). */public void cancel() {if (!mCanceled && mCancelMessage != null) {mCanceled = true;// Obtain a new message so this dialog can be re-usedMessage.obtain(mCancelMessage).sendToTarget();}dismiss();}重写下onTouch()方法就可以了。代码如下
@Overridepublic boolean onTouchEvent(MotionEvent event) {if (isShowing() && shouldCloseOnTouch(getContext(),event)){ViewHelper.hideKeyboard(context, et_reply_comment);}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));} // 关闭键盘public static void hideKeyboard(Context context, EditText et) {InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);LogUtils.showLog("hideKeyboardimm.isActive() = "+imm.isActive()+"et.getWindowToken() = "+et.getWindowToken());if (imm.isActive()) {imm.hideSoftInputFromWindow(et.getWindowToken(), 0);}}经测试,以上两种方法都可以关闭软键盘。
@Overrideprotected void onStart() {super.onStart();getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);}以上所述是小编给大家介绍的dialog dismiss时键盘不消失的问题浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!