无法阻止 EditText 在 activity 开始时获取焦点

Can't prevent EditText from grabbing focus on activity start

我有以下结构的布局(为简洁起见省略了一些项目和属性):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical">

    <ScrollView android:orientation="vertical">

        <TableLayout android:layout_width="match_parent"
            android:layout_height="wrap_content">

            ...

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Label" android:textAppearance="?android:attr/textAppearanceMedium"/>

                <RelativeLayout
                    android:descendantFocusability="beforeDescendants"
                    android:focusableInTouchMode="true"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/btnSaveStep"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:text="Save"/>

                    <EditText
                        android:id="@+id/etStep"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toLeftOf="@id/btnSaveStep"
                        android:ems="10"
                        android:textAlignment="viewEnd"
                        android:inputType="numberDecimal"
                        android:selectAllOnFocus="true"
                        android:text="10"/>

                </RelativeLayout>

            </TableRow>

            ...

        </TableLayout>
    </ScrollView>

    <Button
        android:id="@+id/btnProceed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Proceed"/>
</LinearLayout>

请注意 RelativeLayoutandroid:descendantFocusability="beforeDescendants"android:focusableInTouchMode="true" 属性 - 它们没有任何效果。我也尝试了 this solution,并将该虚拟布局放置在布局的许多地方 - 都无济于事。

我遇到了同样的问题,并且几乎尝试了所有方法。这最终对我有用。

 private class EditTextFocusListener implements View.OnFocusChangeListener {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (v.equals(inputEmail)) {
            if (!hasFocus) hideKeyboard(inputEmail);
        } else if (v.equals(inputPassword)) {
            if (!hasFocus) hideKeyboard(inputPassword);
        }
    }

    private void hideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

  EditTextFocusListener focusListener = new EditTextFocusListener();
  inputEmail.setOnFocusChangeListener(focusListener);
  inputPassword.setOnFocusChangeListener(focusListener);

将此代码放入您的清单文件

<activity
        android:name=".YourActivity"
        android:label="@string/title_activity"
        android:windowSoftInputMode="stateHidden" />