0

Есть 2 TextView, в textColor из папки drawable я вставил xml файл

text.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_2" android:state_checked="true" />
    <item android:color="@color/color_2" android:state_pressed="true" />
    <item android:color="@color/color_2" android:state_activated="true" />
    <item android:color="@color/color_0" />
</selector>

То есть изначальный цвет черный, при нажатии становится допустим красный, когда отпускаешь обратно становится черный. Так вот, можно ли как-то сделать так чтобы по нажатию на один TextView, красным становились оба TextView а при отпускании опять становились чёрными.

main.xml

<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:textColor="@drawable/text"
        android:textSize="30sp"
        android:textStyle="bold"
        android:clickable="true"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:textColor="@drawable/text"
        android:textSize="30sp"
        android:textStyle="bold"
        android:clickable="true"/>
    </LinearLayout>
</LinearLayout>
1

1 ответ 1

1

Напишите функцию в которой вы находите оба text view и выставьте в них необходимый цвет далее в onCrrate у каждого объекта вызвать

textView.setOnClickListener(v - >{
    Ваш метод();
}) ;
3
  • по сути получилось но как сделать чтобы при отжатии текст становился обратно чёрным. Я понимаю что нужно использовать else но что в нем писать? Если в if я использую isPressed то в else я должен использовать...?
    – Karpuha
    26 июл 2020 в 19:42
  • developer.alexanderklimov.ru/android/views/button.php тогда используй OnTouchListener(): и смотри на состояние кнопки через event.
    – LeonSolya
    28 июл 2020 в 7:02
  • @Karpuha ответил на вопрос но забыл упоминание прикрепить
    – LeonSolya
    28 июл 2020 в 7:32

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.