[英]Show / hide Android softkeyboard on fragment replace
我有一个片段活动。 让我们说一个包含事物列表的列表片段。 现在我想让用户添加一个东西,所以我使用FragmentManager用一个带有EditText的插入片段替换列表片段。 EditText具有焦点,光标闪烁。 但软键盘无法打开。 反之亦然:如果用户输入了新内容并将其添加到列表中,我将插入片段替换为列表片段。 但是,虽然没有EditText,键盘也不会关闭。
实现这个的正确方法是什么? 我不敢相信我必须在所有过渡时手动显示和隐藏键盘?!
我想跟随的事情:
1.扩展Fragment
类
2.覆盖onAttach()
和onDetach()
回调
3.实现显示和隐藏软件键盘方法
示例代码:
class MyFragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//show keyboard when any fragment of this class has been attached
showSoftwareKeyboard(true);
}
@Override
public void onDetach() {
super.onDetach();
//hide keyboard when any fragment of this class has been detached
showSoftwareKeyboard(false);
}
protected void showSoftwareKeyboard(boolean showKeyboard){
final Activity activity = getActivity();
final InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), showKeyboard ? InputMethodManager.SHOW_FORCED : InputMethodManager.HIDE_NOT_ALWAYS);
}
}
要显示键盘:
editText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);
然后隐藏它:
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
我的app有同样的问题,最后我们有2个选项,首先创建一个通用函数并在所有转换中调用它或创建一个全局转换如何:
public static void RemoveAndReplaceFragment(FragmentManager fragmentManager, int FragmentContent, Fragment PlaceHolderFragment,Activity context){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.frgMainActivity))
.commit();
//Close keyBoard in transition
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.animation_fade_in,R.anim.animation_fade_out);
fragmentTransaction.replace(R.id.frgMainActivity, new PlaceholderFragment_MainActivity_AcceptAndFollowTask()).commit();
}
}
最好的方法是捕捉过渡事件,但我们不能在这一刻......告诉我,如果我帮助你,祝你好运!
这肯定会起作用:
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
private void showKeyboard(){
EditText myEditText = (EditText) findViewById(R.id.myEditText);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
我猜你的ListView
正在窃取你的EditText
的焦点,尝试这个,让我知道它是否有帮助。
getListView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
尝试这个
InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
editText.requestFocus();
我同意,我不敢相信你也必须手动隐藏键盘。
将此添加到Fragment onCreate,您不希望键盘不在:
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mToolbar.getWindowToken(), 0);
将mToolbar更改为布局中的任何视图。 在这种情况下,我使用我的工具栏。
将其添加到您希望键盘显示的Fragment onCreate。
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
您可以使用以下代码隐藏软键盘。
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
一种方法是调用Activity.recreate
,因为无论如何都需要处理方向改变的情况。 看起来像矫枉过正,你最终也会得到不同的过渡动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.