案例1:如果您想在打开对话框片段时关闭键盘
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = super.onCreateView( inflater, container, savedInstanceState );
//to hide keyboard when showing dialog fragment
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return view;
}
案例2:如果您想在选择自动完成文本或任何其他编辑文本视图上关闭键盘,请使用简单
public static void hideDialogFragmentKeyboard(Context context,View view) {
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}, 100);
}
我认为这会奏效