繁体   English   中英

点击textview时如何改变背景颜色和文字颜色?

[英]How to change the background color and text color when textview is clicked?

这是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 {

            }
        }
    }
})

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

有没有更好的办法?

用于在单击时更改按钮背景颜色,请使用此独奏

按钮 XML 代码: step1:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM