在Android应用程序中未定义InputMethodManager关键字

Jay*_*Jay 3 android keyword inputmethodmanager

我试图使用以下代码隐藏用户单击按钮时的软键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

InputMethodManager未定义

我想我需要添加一个import语句?如果是这样,有谁知道什么?或者我该如何解决这个问题?

Ale*_*s G 5

InputMethodManager在包中android.view.inputmethod,所以你需要添加

import android.view.inputmethod.InputMethodManager;
Run Code Online (Sandbox Code Playgroud)

到你的班级.

  • 在将来,如果您正在使用eclipse,请按(在Mac上)Command + Shift + o以解析所有导入(以及Windows上的Control + Shift + o) (2认同)