在碎片替换上显示/隐藏Android软键盘



我有一个带有片段的"活动"。让我们说一个列表片段,其中包含一个事物列表。现在我想让用户添加一个东西,所以我使用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);
   }
}

我的应用程序也有同样的问题,最后我们有两个选项,首先创建一个通用函数并在所有转换中调用它,或者创建一个全局转换如何:

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

我同意,我不敢相信你也必须手动隐藏键盘。

将此添加到您不希望键盘不在的片段创建中:

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

对于Kotlin:

  1. 把这个函数放在类似Utils类的地方
fun hideSoftKeyboard(view: View, context: Context) {
        val inputMethodManager = 
            context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }

然后在碎片或活动中使用,如:


Utils().hideSoftKeyboard(inputTextChat, requireContext())

一种方法是调用Activity.recreate,因为无论如何都需要处理方向更改情况。看起来太夸张了,而且你最终也会得到不同的过渡动画。

最新更新