无法阻止EditText将注意力集中在活动开始上

时间:2016-08-11 14:29:50

标签: android android-edittext focus

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

<?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>

请注意android:descendantFocusability="beforeDescendants"的{​​{1}}和android:focusableInTouchMode="true"属性 - 它们无效。我也试过this solution,然后把那个虚拟布局放在布局的许多地方 - 都无济于事。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并尝试了一切。这最终对我有用。

 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);

答案 1 :(得分:1)

将此代码放入清单文件

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