Скрыть TextView, когда SearchView открыт

Я использую TextView поверх SearchView в качестве заголовка. Другой вариант — настроить подсказку поискового запроса, изменив шрифт, цвет, размер текста и центрирование. Это кажется более простым методом.

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SearchView
            android:id="@+id/searchview"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_marginStart="12dp"
            android:layout_marginTop="40dp"
            android:layout_marginEnd="12dp"
            android:background="@drawable/searchbackground"
            android:closeIcon="@drawable/search_x_button"
            android:iconifiedByDefault="false"
            android:paddingStart="2dp"
            android:paddingTop="11dp"
            android:paddingEnd="15dp"
            android:paddingBottom="11dp"
            android:queryBackground="@android:color/transparent"
            android:gravity="center"
            android:searchIcon="@drawable/ic_ssearch"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/textView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="49dp"
            android:layout_marginEnd="24dp"
            android:fontFamily="@font/manrope_semibold"
            android:text="@string/search"
            android:textColor="@color/font_title"
            android:textSize="19sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

Как я могу скрыть текстовое представление, когда поисковое представление находится в фокусе, то есть пользователь нажимает на него, и появляется мигающий курсор?

Изменить: исправлено, сделав это

searchHint = view.findViewById(R.id.textView13);

а потом

public boolean onQueryTextChange(String query) {
            if (query.equals("")) {
                searchHint.setVisibility(View.VISIBLE);
            } else {
                searchHint.setVisibility(View.GONE);
            }
            return true;
        }

person MangoBot    schedule 01.06.2020    source источник


Ответы (2)


Вы можете попробовать это так:

Всякий раз, когда вы нажимаете на TextView, изменяйте его видимость на GONE и добавляйте фокус в SearchView.

SearchView mSearchView = findViewById(R.id.searchview);
TextView textView = findViewById(R.id.textView13)

textView.setOnClickListener(v -> {
     textView.setVisibility(View.GONE);
     mSearchView.setSearchFocused(true);
  });

Надеюсь, поможет!

person Ronak Sethi    schedule 02.06.2020

Если я вас правильно понял, вы просто хотите скрыть текст подсказки (@+id/textView13), когда пользователь нажимает на SearchView(@+id/searchview). Если это так, кажется, что вы можете добавить приведенные ниже коды (вы можете проверить, соответствует ли ваше требование)

    searchview.setOnQueryTextFocusChangeListener { _, hasFocus ->
        if (hasFocus) {
            textView13.visibility = View.INVISIBLE
        } else {
            textView13.visibility = View.VISIBLE
        }
    }
person 3kakim    schedule 02.06.2020