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

问题描述 投票:0回答:3

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

android android-edittext setfocus
3个回答
0
投票

从类中添加这些行,

EditText.isFocusableInTouchMode();
EditText.setFocusable(true); 
EditText.requestFocus(); 

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

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

0
投票

在您的xml中设置此值

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

您可以在onViewCreated程序editText.isFocusableInTouchMode(); editText.setFocusable(true);上进行设置


0
投票

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了解详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.