Android Studio - 输入法管理器

时间:2017-08-25 03:36:42

标签: android android-studio webview inputmethodmanager

Android studio版本:2.3.3 下面的代码不起作用,它应该隐藏键盘但不是。请帮助。

 InputMethodManager imm = (InputMethodManager) 
    getSystemService(Context.INPUT_METHOD_SERVICE);
    public void setImm(InputMethodManager imm) {
    this.imm = imm;
    }
    public InputMethodManager getImm() {
    imm.hideSoftInputFromWindow(urledit.getWindowToken(),0);
    return imm;
    }

3 个答案:

答案 0 :(得分:0)

/**
 * Hides the soft keyboard
 */
 public void hideSoftKeyboard() {
   if(getCurrentFocus()!=null) {
   InputMethodManager inputMethodManager = (InputMethodManager) 
 getSystemService(INPUT_METHOD_SERVICE);
   inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
  }
}

 /**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
   InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
   view.requestFocus();
   inputMethodManager.showSoftInput(view, 0);
}

尝试隐藏并显示键盘

答案 1 :(得分:0)

希望有用

public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
    view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

答案 2 :(得分:0)

嗨,使用此代码,它可以100%工作

            View.OnTouchListener disable = new View.OnTouchListener() {
            public boolean onTouch (View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager img = 
           (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
            if (img != null) {
            img.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
            System.out.println(ee.getSelectionEnd());
            return true;
           }
           };
           //create a Button to setOnTouchListener(event)
           Button ee=new Button(this);
           ee.setOnTouchListener(disable);