@lacredin
Программист

Как отключить реакцию на касание для EditText в Android?

Мне надо отключить реакцию EditText на касание.
Я уже использовал
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"

Но при касании, всё равно на короткое время появляется курсор и меняет цвет нижняя линия EditText.
EditText я использую в связке с TextInputLayout, вот код:
<android.support.constraint.ConstraintLayout
                android:id="@+id/fragment_phone_registration_profile_box"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:focusableInTouchMode="true"
                >
 <android.support.design.widget.TextInputLayout
                    android:id="@+id/fragment_fio_input_surname_info"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:hint="@string/fragment__phone_registration_FIO"
                    android.support.design:hintAnimationEnabled="true"
                    android.support.design:hintEnabled="true"
                    android.support.design:layout_marginTop="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/image_fio"
                    app:layout_constraintTop_toTopOf="parent"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:clickable="false"
                    >

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/fragment_phone_registration_profile_text"
                        style="@style/R16"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:maxLength="200"
                        android:maxLines="4"
                        android:focusable="false"
                        android:clickable="false"
                        android:focusableInTouchMode="false"
                        />
                </android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>


В идеале хотелось бы, чтобы обработка нажатий полностью уходила к ConstraintLayout.
Подскажите, можно ли это сделать без использования костылей, на подобии, изменения стиля цвета курсора на прозрачный?
  • Вопрос задан
  • 992 просмотра
Пригласить эксперта
Ответы на вопрос 2
@Tiberal
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"


для TextInputEditText
Ответ написан
Комментировать
@YuryBorodkin
Android dev
Для таких случаев пригодиться метод onInterceptTouchEvent(MotionEvent) класса viewgroup.
Создадим класс-наследник вьюгрупп
public class InterceptableConstraintLayout extends ConstraintLayout {
    private Set<View> mIntercepatbleViews = new HashSet<>();

    public InterceptableConstraintLayout(Context context) {
        super(context);
    }

    public InterceptableConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InterceptableConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void addViewToIntercept(View v) {
        if (v == null) return;
        mIntercepatbleViews.add(v);
    }

    public void removeViewFromInterception(View v) {
        if (v == null) return;
        mIntercepatbleViews.remove(v);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN
                && mIntercepatbleViews.size() > 0
                && getChildCount() > 0) {
            int x = (int) ev.getX();
            int y = (int) ev.getY();
            Rect hitRect = new Rect();
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                child.getHitRect(hitRect);
                if (hitRect.contains(x, y) && mIntercepatbleViews.contains(child)) return true;
            }
            return super.onInterceptTouchEvent(ev);
        } else return super.onInterceptTouchEvent(ev);
    }
}


в activity берем нужные вью и передаем их в метод addViewToIntercept(View)
InterceptableConstraintLayout mLo = findViewById(R.id.fragment_phone_registration_profile_box);
        View mEt = findViewById(R.id.fragment_fio_input_surname_info);
        mLo.addViewToIntercept(mEt);

        mLo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("mLo", "clicked");
            }
        });

В результате клики на нужные вьюхи перехватываются.
Добиться чего-то похожего через xml - я хз как. android:descendantFocusability="beforeDescendants" и clickable = true тоже не помогут.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы