当MyFgment
出现在屏幕上时,我想将光标聚焦自动设置在EditText
中的MyFragment
上。我已经尝试过EditText.requestFocus()
,但它并不专注于EditText
。我该怎么办?
从类中添加这些行,
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了解详细信息。