finish activity收回软键盘

editText获取焦点后,会自动弹出软键盘,关闭的时候一般需要按返回键或者点击软键盘上的按钮,即使当前activity已经finish掉,软键盘依然存在

在网上找了两种可用的方法, 在此记录:

//此方法只是关闭软键盘 可以在finish之前调用一下
private void hintKbTwo() {
 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);            
 if(imm.isActive()&&getCurrentFocus()!=null){
    if (getCurrentFocus().getWindowToken()!=null) {
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }             
 }
}
//此方法,如果显示则隐藏,如果隐藏则显示
private void hintKbOne() {
    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);    
    // 得到InputMethodManager的实例
    if (imm.isActive()) {
         // 如果开启
    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
    InputMethodManager.HIDE_NOT_ALWAYS);

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章