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

编辑文本列表视图

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

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

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


当前回答

当按下按钮时,我使用以下代码阻止EditText窃取焦点。

addButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        View focused = internalWrapper.getFocusedChild();
        focused.setVisibility(GONE);
        v.requestFocus();
        addPanel();
        focused.setVisibility(VISIBLE);
    }
});

Basically, hide the edit text and then show it again. This works for me as the EditText is **not** in view so it doesn't matter whether it is showing.

You could try hiding and showing it in succession to see if that helps it lose focus.

其他回答

在Manifest文件的下面一行添加您提到的活动

android:windowSoftInputMode="stateAlwaysHidden"

您可以通过使用请求焦点并使用键盘隐藏代码来指定其他小部件的焦点。

当按下按钮时,我使用以下代码阻止EditText窃取焦点。

addButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        View focused = internalWrapper.getFocusedChild();
        focused.setVisibility(GONE);
        v.requestFocus();
        addPanel();
        focused.setVisibility(VISIBLE);
    }
});

Basically, hide the edit text and then show it again. This works for me as the EditText is **not** in view so it doesn't matter whether it is showing.

You could try hiding and showing it in succession to see if that helps it lose focus.

问题似乎来自我只能在布局的XML形式中看到的属性。

确保在EditText XML标记中删除声明末尾的此行:

<requestFocus />

这应该是这样的:

<EditText
   android:id="@+id/emailField"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailAddress">

   //<requestFocus /> /* <-- without this line */
</EditText>

try

edit.setInputType(InputType.TYPE_NULL);

edit.setEnabled(false);