我在Android中有一个活动,包含两个元素:

编辑文本列表视图

当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:

EditText.setSelected(false);
EditText.setFocusable(false);

没有运气。如何说服EditText在“活动”启动时不选择自己?


当前回答

在onCreate方法中添加以下内容:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

其他回答

我找到的唯一解决方案是:

创建LinearLayout(我不知道其他类型的Layout是否有效)设置属性android:focusable=“true”和android:focuableInTouchMode=“true“

并且EditText在开始活动后无法获得焦点

在父版面中写入此行。。。

 android:focusableInTouchMode="true"

在onCreate方法中添加以下内容:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
View current = getCurrentFocus();

if (current != null) 
    current.clearFocus();

当您的活动打开时,键盘会自动可见,这会导致EditText聚焦。您可以通过在manifest.xml文件的活动标记中写入以下行来禁用键盘。

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"