a-b.top 2019-08-21 22:52 采纳率: 0%
浏览 1313
已采纳

android 用代码关闭启动软键盘无效

/**
* 隐藏输入软键盘
* @param context
* @param view
*/
public static void hideInputManager(Context context,View view){
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view !=null && imm != null){
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏
//imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//imm.hideSoftInputFromWindow(view.getWindowToken(), 0x0002);
}
}
public void hideInputMethod() {
try {
getWindow().getDecorView().findFocus().clearFocus(); //清理焦点
} catch (Exception e) {
}
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
System.out.println("开始了。。。。。。");
if (imm.isActive() && this.getCurrentFocus() != null) {
System.out.println("我进来了啊。。。。");
if (this.getCurrentFocus().getWindowToken() != null) {
System.out.println("执行了啊。。。。。");
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}

    /*InputMethodManager imm = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.hideSoftInputFromWindow(a.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }*/
}


    另在AndroidManifest.xml里面也添加过android:windowSoftInputMode="stateAlwaysHidden"
    网上能用的方法都试过,都是无效果,另外还换过手机,重启一个新项目也没有效果
  • 写回答

1条回答 默认 最新

  • 王能 2019-08-23 14:53
    关注

    hideInputManager至少要等0.5秒才能调用,传的view也必须是EditText
    AndroidManifest.xml里面也添加过android:windowSoftInputMode="stateAlwaysHidden"这个属性肯定有用,表示打开Activity时默认不弹出键盘,你看看是不是代码中不小心调用了requestFocused

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog