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

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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题