dialog dismiss时键盘不消失的问题浅析及解决办法

dialog dismiss时键盘不消失的问题浅析及解决办法,第1张

概述当setCanceledOnTouchOutside(true),点击阴影处,dialogdismiss时键盘消失的问题。如图

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("hIDeKeyboard  imm.isActive() = "+imm.isActive()+"    et.getwindowToken() = "+et.getwindowToken());    if (imm.isActive()) {      imm.hIDeSoftinputFromWindow(et.getwindowToken(),0);    }  }

这是因为当ondismiss 方法执行的时候,dialog已经消失了。已经获取不到windowToken了。

目前发现有两种方式觉得这样的问题

1.在ondismiss()方法里面这样隐藏软键盘

 @OverrIDe  public 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-used      Message.obtain(mCancelMessage).sendToTarget();    }    dismiss();  }

重写下ontouch()方法就可以了。代码如下

 @OverrIDe  public 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,0);    }  }

经测试,以上两种方法都可以关闭软键盘。

另外附在dialog启动时d出软键盘代码,重写onStart方法

@OverrIDe  protected voID onStart() {    super.onStart();    getwindow().setSoftinputMode(WindowManager.LayoutParams.soFT_input_STATE_ALWAYS_VISIBLE);  }

以上所述是小编给大家介绍的dialog dismiss时键盘不消失的问题浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的dialog dismiss时键盘不消失的问题浅析及解决办法全部内容,希望文章能够帮你解决dialog dismiss时键盘不消失的问题浅析及解决办法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存