当 DialogFragment 中的微调器出现 select 项时隐藏键盘
Hide Keyboard when select item from spinner in DialogFragment
我试图在从 Spinner
中选择一个项目后隐藏键盘,但代码不工作并且没有任何反应。但在另一边,相同的代码在正常片段中工作。
隐藏键盘的方法如下:
public static void hideKeypad(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
这在片段中对我有用
public void removePhoneKeypad() {
if(getActivity().getCurrentFocus()!=null &&getActivity().getCurrentFocus().getWindowToken() != null) {
System.out.println("getCurrentFocus() in frag");
InputMethodManager inputManager = (InputMethodManager) rootView
.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = getActivity().getCurrentFocus().getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
您需要指定 Fragment
的 rootView
因为在您的方法中它看到了 getCurrentFocus() == null
,所以它永远不会转到代码的其余部分。
这是正确的代码:
public static void hideKeypad(Activity activity, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
创建一个 class 变量 View
并将其等于 onCreateView()
中 Fragment
的 rootView
并在此 Fragment
.
我试图在从 Spinner
中选择一个项目后隐藏键盘,但代码不工作并且没有任何反应。但在另一边,相同的代码在正常片段中工作。
隐藏键盘的方法如下:
public static void hideKeypad(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
这在片段中对我有用
public void removePhoneKeypad() {
if(getActivity().getCurrentFocus()!=null &&getActivity().getCurrentFocus().getWindowToken() != null) {
System.out.println("getCurrentFocus() in frag");
InputMethodManager inputManager = (InputMethodManager) rootView
.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = getActivity().getCurrentFocus().getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
您需要指定 Fragment
的 rootView
因为在您的方法中它看到了 getCurrentFocus() == null
,所以它永远不会转到代码的其余部分。
这是正确的代码:
public static void hideKeypad(Activity activity, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
创建一个 class 变量 View
并将其等于 onCreateView()
中 Fragment
的 rootView
并在此 Fragment
.