在 OnViewCreated 中打开键盘

open keyboard in OnViewCreated

我有一个片段,里面有一个 EditText,我只想在到达这个片段时自动打开键盘,但我找不到路。

我想 java 提供了一种方法,但我自己找不到。

谢谢。

在片段的 onCreateView() 方法中添加这一行

    InputMethodManager in = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            in.hideSoftInputFromWindow(getApplicationWindowToken(), 0);

或使用 ,

android:focusable="true"

在 xml 的编辑文本中

您可以在 onViewCreated() 中请求焦点

.....
 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    EditText editText = view.findViewById(R.id.et);
    editText.requestFocus();
    InputMethodManager imgr = (InputMethodManager) 
    getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
...