简体   繁体   English

在片段替换时显示/隐藏Android软键盘

[英]Show / hide Android softkeyboard on fragment replace

I have an Activity with a fragment. 我有一个片段活动。 Let's say a list fragment with a list of things. 让我们说一个包含事物列表的列表片段。 Now I want to let the user add a thing, so I use the FragmentManager to replace the list fragment with an insert fragment which has an EditText. 现在我想让用户添加一个东西,所以我使用FragmentManager用一个带有EditText的插入片段替换列表片段。 The EditText has the focus and the cursor is blinking. EditText具有焦点,光标闪烁。 But the softkeyboard doesn't open. 但软键盘无法打开。 Same thing other way round: if the user has entered the new thing and added it to the list, I replace the insert fragment back with a list fragment. 反之亦然:如果用户输入了新内容并将其添加到列表中,我将插入片段替换为列表片段。 But although there is no EditText anymore, the keyboard doesn't close. 但是,虽然没有EditText,键盘也不会关闭。

What is the correct way to implement this? 实现这个的正确方法是什么? I can't believe that I have to show and hide the keyboard manually on all transitions?! 我不敢相信我必须在所有过渡时手动显示和隐藏键盘?!

I would to following things: 我想跟随的事情:

1. Extend Fragment class 1.扩展Fragment
2. Override onAttach() and onDetach() callbacks 2.覆盖onAttach()onDetach()回调
3. Implement show and hide software keyboard method 3.实现显示和隐藏软件键盘方法

sample code: 示例代码:

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);
   }
}

To show the keyboard: 要显示键盘:

editText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);

and then to hide it: 然后隐藏它:

InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);

I have the same problem with my app and finally we have 2 options, at first create a general function and call this in all transitions or create a global transition how this: 我的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();
    }
}

The best way is capture the transition event but we can't in this moment... Tell me if I helps you, good luck! 最好的方法是捕捉过渡事件,但我们不能在这一刻......告诉我,如果我帮助你,祝你好运!

This will work for sure: 这肯定会起作用:

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);

Try this 尝试这个

InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

    imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    editText.requestFocus();

I agree, I couldn't believe that you had to manually hide the keyboard as well. 我同意,我不敢相信你也必须手动隐藏键盘。

Add this to your Fragment onCreate that you do not want the Keyboard not to be in: 将此添加到Fragment onCreate,您不希望键盘不在:

   ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mToolbar.getWindowToken(), 0);

change mToolbar to any view within the layout. 将mToolbar更改为布局中的任何视图。 In this case I used my toolbar. 在这种情况下,我使用我的工具栏。

Add this to the Fragment onCreate that you want the keyboard to be shown. 将其添加到您希望键盘显示的Fragment onCreate。

        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

you can use below code to hide soft keyboard. 您可以使用以下代码隐藏软键盘。

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

One way is to call Activity.recreate , since it is necessary to handle the orientation-change case anyway. 一种方法是调用Activity.recreate ,因为无论如何都需要处理方向改变的情况。 Seems like overkill, and you also end up with different transition animations. 看起来像矫枉过正,你最终也会得到不同的过渡动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM