Change clickable TextView's color on focus and click?

后端 未结 8 1368
灰色年华
灰色年华 2020-11-28 05:26

I have a clickable TextView that I want to give some colors to. But I don\'t know how. Here are the relevant code snippets from my two files that I\'m working with:

相关标签:
8条回答
  • 2020-11-28 05:57

    It's very easy.Try this one.. It worked for me:

    File name: res/color/bg_tab_text_color.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_pressed="true" android:drawable="@color/blue" />
    

    Try setting the color in xml layout as:

    android:textColor="@color/bg_tab_text_color"
    
    0 讨论(0)
  • 2020-11-28 06:03

    Here is a very simple way programmatically:

    private void setColorStateList(TextView view) {
            int[][] states = new int[][] {
                    new int[] { android.R.attr.state_pressed}, // pressed
                    new int[] { android.R.attr.state_focused}, // focused
                    new int[] { android.R.attr.state_enabled}  // enabled
            };
    
            int[] colors = new int[] {
                    getResources().getColor(R.color.blue),
                    getResources().getColor(R.color.green),
                    getResources().getColor(R.color.green) 
            };
    
            ColorStateList list = new ColorStateList(states, colors);
            view.setTextColor(list);
            view.setClickable(true);
            view.setFocusableInTouchMode(true);
        }
    
    0 讨论(0)
  • 2020-11-28 06:07

    In res/color/text_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/white"
              android:state_selected="true"/>
        <item android:color="#8020e0"/>
    </selector>
    

    In layout:

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Word"
        android:textColor="@color/text_selector"
        android:textSize="12sp"/>
    

    In code:

    textView.setSelected(false); // To deselect.
    textView.setSelected(true);
    
    0 讨论(0)
  • 2020-11-28 06:10

    If you want to set stateful color from code, you need to pass in ColorStateList as an argument to setTextColor passing an int to the method results in setting the color to all the states. It also looks like your xml is not totally correct. Example from ColorStateList docs looks like(should be located like this: res/color/selector_txt.xml):

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:color="@color/testcolor1"/>
        <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
        <item android:state_enabled="false" android:color="@color/testcolor3" />
        <item android:color="@color/testcolor5"/>
     </selector>
    

    UPD on how to set a ColorStateList to text color:

    ColorStateList cl = null;
    try {
       XmlResourceParser xpp = getResources().getXml(R.color.selector_txt);
       cl = ColorStateList.createFromXml(getResources(), xpp);
    } catch (Exception e) {}
    

    Note: The method createFromXml(Resources, XmlPullParser parser) was deprecated in API level 23. Use createFromXml(Resources, XmlPullParser parser, Theme)

    With XML its as easy as:

    android:textColor="@color/selector_txt"
    
    0 讨论(0)
  • 2020-11-28 06:14

    In res/drawable/tab_textColor.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="#FFFFFF" />
        <item android:state_pressed="true" android:color="#FFFFFF" />
        <item android:color="#CCCCCC" />
    </selector>
    
    
    
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="@string/tab_indicator_home"
        android:textColor="@drawable/tab_textcolor"
        android:textSize="10sp"
        android:visibility="visible"
        tools:ignore="SmallSp" />
    
    0 讨论(0)
  • 2020-11-28 06:19

    Step 1: Set the text color in xml like this

    android:textColor="@color/text_color"

    Step2: Create res/color/text_color.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:state_pressed="true"
          android:color="#ffffffff"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/>  <!--default -->
    
    </selector>
    
    0 讨论(0)
提交回复
热议问题