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