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

编辑文本列表视图

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

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

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


当前回答

在onCreate()中禁用它

final KeyListener edtTxtMessageKeyListener = edtTxtMessage.getKeyListener();
edtTxtMessage.setCursorVisible(false);
edtTxtMessage.setKeyListener(null);

最后在EditText的onClick()中启用它

edtTxtMessage.setCursorVisible(true);
edtTxtMessage.setKeyListener(edtTxtMessageKeyListener);

但问题是,我们必须在第一次使用OnScreenKeyboard时单击两下。

@变通办法

InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

也可以在onClick()中尝试:)

其他回答

对我来说,在所有设备上工作的是:

    <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->

    <View
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

只需将此作为一个视图放在有问题的聚焦视图之前,就可以了。

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

 android:focusableInTouchMode="true"

我需要以编程方式明确关注所有领域。我刚刚在主布局定义中添加了以下两条语句。

myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);

就这样。立刻解决了我的问题。谢谢,西尔弗,你给我指明了正确的方向。

简单的解决方案:在活动标签中的AndroidManifest中使用

android:windowSoftInputMode="stateAlwaysHidden"

迟到了,但可能会有所帮助。在布局顶部创建一个虚拟的EditText,然后在onCreate()中调用myDummyEditText.requestFocus()

<EditText android:id="@+id/dummyEditTextFocus" 
android:layout_width="0px"
android:layout_height="0px" />

这似乎和我预期的一样。无需处理配置更改等。我需要这一点用于具有冗长文本视图(说明)的“活动”。