android textview Click to change color

Keywords: Android xml

 

1. Click textview to change color and release to restore. The effect is shown in the left figure:

2. No recovery after loosening. As shown in the right picture:

                                                              

 

1, textview Click to change color, release to restore

1. Set clickable property to true -----Textview is not clickable by default

android:clickable="true"

2. Create a new selector type xml file under drawable file. My name is demo [selector]

Click the property of textview to set it as "state" and "pressed"

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:color="#3D4245"></item>
    <item android:state_pressed="true" android:color="#FF5000"></item>
</selector>

3. Add reference

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="@drawable/demo_selector"/>
            <TextView

Done.

2, textview Click to change color, release without recovery

I'll use the Radiogroup control for this effect because it comes with the selected properties.

1. Set layout

Adding a RadioGroup contains two RadioButton controls

2. Create a new selector type xml file under drawable file. My name is demo [selector]

Because it is a RadioButton, different from textview, the property here is state "U checked

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:color="#3D4245"></item>
    <item android:state_checked="true" android:color="#FF5000"></item>
</selector>

3. Set style

Open the styles.xml file under the values file

                

Add your own style

    <style name="demoStyle">
        <item name="android:button">@null</item>
        <item name="android:textSize">17sp</item>
        <item name="android:textColor">@drawable/demo_selector</item>  //Reference selector
    </style>

Done.

The layout page is set as follows:

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Price"
                    style="@style/demoStyle"/>
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_height="wrap_content"
                    android:text="Sales volume"
                    style="@style/demoStyle"/>

            </RadioGroup>

 

Posted by blogger3 on Mon, 06 Jan 2020 23:35:54 -0800