【问题标题】:How to change the background color and text color when textview is clicked?单击textview时如何更改背景颜色和文本颜色?
【发布时间】:2021-05-02 06:38:41
【问题描述】:

这是RecyclerView中使用的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wordLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:background="@drawable/textview_pressed_background"
        android:gravity="left|center_vertical"
        android:padding="10dp"
        android:text="Android Studio"
        android:textColor="@color/textview_pressed_textcolor"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:gravity="right|center_vertical"
        android:padding="10dp"
        android:text="@string/hide_kor_string"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />
</LinearLayout>

这是textview_pressed_background.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#C89EC4" />
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</selector>

这是textview_pressed_textcolor.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FFFFFF" android:state_pressed="true" />

    <item android:color="#000000" android:state_pressed="false" />
</selector>

当我按下textView1 时,我想将所有视图的背景颜色更改为相同颜色,并将所有 TextView 的文本颜色更改为相同颜色。

所以当我想到自己时,我想到了在 View.OnTouchListener 中使用MotionEvent 的方式。

TextView.setOnTouchListener(object : View.OnTouchListener {
    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        val action = event?.action
        when (action) {
            action -> MotionEvent.ACTION_DOWN {

            }
            action -> MotionEvent.ACTION_CANCEL {

            }
            action -> MotionEvent.ACTION_UP {

            }
        }
    }
})

但我寻求帮助是因为我认为会有更好的方法我不知道。

有没有更好的办法?

【问题讨论】:

    标签: android xml android-studio


    【解决方案1】:

    点击后更改按钮背景颜色,使用此独奏

    按钮 XML 代码: 第一步:

    <android.support.constraint.ConstraintLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".MainActivity">
       <Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click here to change color!"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:ignore="HardcodedText" />
    </android.support.constraint.ConstraintLayout>
    

    第 2 步: MainActivity 中的初始化按钮:

     Button button = findViewById(R.id.button);
          button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
             }
          });
       }
    }
    

    最后一步:更改清单

     <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme">
          <activity android:name=".MainActivity">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
          </activity>
       </application>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 2011-06-05
      • 2013-09-27
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多