我想隐藏在android中的片段中的键盘.因为一旦它显示它仍然在所有片段中可见.我尝试这种方法
public static void hideKeyboard(Context ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View v = ((Activity) ctx).getCurrentFocus();
if (v == null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
并在按钮单击时调用此方法
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard(ctx);
login();
}
});
Run Code Online (Sandbox Code Playgroud)
但这会给出错误"java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'"