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

编辑文本列表视图

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

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

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


当前回答

这是最完美、最简单的解决方案。我总是在我的应用程序中使用这个。getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

其他回答

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

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

利用其他海报提供的信息,我使用了以下解决方案:

在布局XML中

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:id="@+id/linearLayout_focus"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>

<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
    android:id="@+id/autocomplete"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:inputType="textNoSuggestions|textVisiblePassword"/>

在onCreate()中

private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    //get references to UI components
    mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}

最后,在onResume()中

@Override
protected void onResume() {
    super.onResume();

    //do not give the editbox focus automatically when activity starts
    mAutoCompleteTextView.clearFocus();
    mLinearLayout.requestFocus();
}

我遇到了这样的问题,这是由于我的选择。第一个状态是焦点,即使我的视图被禁用了,它也采用了焦点状态,因为它是第一个匹配并使用它的状态。您可以像这样将第一个状态设置为禁用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">


<item android:drawable="@drawable/text_field_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/text_field_focused" android:state_focused="true"/>
<item android:drawable="@drawable/text_field_normal"/>

您可以将Editext设置为禁用焦点属性,现在这可以通过两种方式应用:

您可以禁用focusable作为常规属性或者您可以禁用FocusableInTouchMode作为触摸模式(触摸屏)中该视图的特定属性

默认情况下,如果Editext位于该活动中视图堆栈的顶部(例如,标头),则focusable属性为true,那么它将在活动启动时可聚焦。

要禁用Focusable,只需将其布尔值设置为false即可。

因此,这将是:

android:focusable="false"

要禁用FocusableInTouchMode,只需将其布尔值设置为false即可。因此,这将是:

android:focusable="false"

您只需找到要应用更改的Textview,然后将相应的代码片段添加到xml文件中的xml规范中。

或者,您可以单击布局编辑器中的Textview,找到显示该Textview所有xml属性的侧边栏,然后简单地向下滚动到声明“Focusable”和“FocusableInTouchMode”的位置,并检查它们是否为true或false。

当按下按钮时,我使用以下代码阻止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.