片段开始时如何将焦点设置到editText?

How to set focus to editText when fragment start?

MyFgment出现在屏幕上时,我想自动将光标焦点设置在MyFragment中的EditText上。我试过 EditText.requestFocus(),但它不关注 EditText。我该怎么办??

添加来自 class、

的这些行
EditText.isFocusableInTouchMode();
EditText.setFocusable(true); 
EditText.requestFocus(); 

或者在布局中添加这些属性,

android:focusable="true"
android:focusableInTouchMode="true"

在您的 xml

中设置
android:focusable="true"
android:focusableInTouchMode="true"

你可以在 onViewCreated 程序上设置它 editText.isFocusableInTouchMode(); editText.setFocusable(true);

editText.requestFocus() 会将焦点放在您的 View 上(如果它是可聚焦的)。但我想你想在焦点集中时显示键盘。如果我是对的,那么下面的代码可能对你有用。

editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

代码来自此post。 您也可以查看 Android Developers 了解详情。