在后代clearFocus无法按预期工作之前可调整布局

时间:2017-02-27 11:08:39

标签: android android-layout layout focus

我的表单布局看起来像。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

           <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/small"
                android:layout_marginEnd="@dimen/small"
                android:layout_marginStart="@dimen/small"
                android:layout_marginTop="@dimen/small">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"/>

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/small"
                android:layout_marginEnd="@dimen/small"
                android:layout_marginStart="@dimen/small">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"/>

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/small"
                android:layout_marginEnd="@dimen/small"
                android:layout_marginStart="@dimen/small">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionDone"
                    android:inputType="textPassword" />

            </android.support.design.widget.TextInputLayout>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/small" />

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="@dimen/small"
                        android:layout_marginStart="@dimen/small"
                        android:layout_marginTop="@dimen/small" />

                    <Button
                        style="@style/Button.Small"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/small" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/small"
                        android:layout_marginEnd="@dimen/small"
                        android:layout_marginStart="@dimen/small">

                        <android.support.design.widget.TextInputEditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:imeOptions="actionDone"
                            android:inputType="text" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/small"
                        android:layout_marginEnd="@dimen/small"
                        android:layout_marginStart="@dimen/small">

                        <android.support.design.widget.TextInputEditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:imeOptions="actionDone"
                            android:inputType="text" />

                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

我希望在用户点击窗口时清除EditText的焦点,以便我实现下面的代码

     /**
     * Clear the edit text focus if the touch is outside its limits
     *
     * @param context  the activity context
     * @param editText the edit text
     * @param event    the motion event
     */
    public static void clearEditTextFocusOnMontionEvent(@NonNull Context context, @NonNull EditText editText, @NonNull MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Rect outRect = new Rect();
            editText.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                editText.clearFocus();
                hideKeyboard(context, editText);
            }
        }
    }

但重点不是每次都去LinearLayout。通过第一个可见的EditText获得焦点(当标题TextView可见时,例外,LinearLayout获得焦点)。

我预计LinearLayout总是可以获得焦点,因为它的可聚焦性是在theDescendants之前。

单击acionDone时也一样,我希望从当前的EditText中删除焦点,所以我实现了

if (actionId == EditorInfo.IME_ACTION_DONE) {
            WindowsHelper.hideKeyboard(context, view);
            view.clearFocus();
        }

但是第一个可见的EditText再次获得了焦点。

有人知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

在您的游戏中添加以下库

compile&#39; com.yinglan.keyboard:hidekeyboard:0.0.2&#39;

在onCreate方法中添加以下代码

HideUtil.init(本);

并且setFocus对您的父视图为true