简体   繁体   中英

How to make an Android TextView TextColor stay highlighted?

I have a TextView in a ListView.I want the TextColor of the TextView stay highlighted when selected.It's working fine in newer versions, but in lower versions it's not being highlighted, it's just blinking.

Here is my xml:

listview_bell_items.xml

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:padding="5dip" >

       <TextView
             android:id="@+id/txt_bell_title"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:text="@string/rihanna_love_the_way_lie"
             android:textColor="@drawable/bell_selected_text_color"
             android:textSize="15sp"
             android:textStyle="bold"
             android:typeface="sans" />

       <ImageView
            android:id="@+id/list_bell_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/bronze_bell" />

  </RelativeLayout>

bell_selected_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">

            <item android:state_pressed="true" android:color="@color/light_yellow"/>
             <item android:state_focused="true" android:color="@color/light_yellow"/>
             <item android:state_checked="true" android:color="@color/light_yellow"/>
             <item android:state_selected="true" android:color="@color/light_yellow"/>
             <item android:color="@color/white"/>

     </selector>

1.you can use setAlpha(0 to 255 value) to set in textview so it will highlight or set opacity in android textview.

Use this link: http://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android         

(for opacity set in textview)

2.other option set blink of textview:

public static void blinkView(final TextView txtView) {

        Animation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(400); // You can manage the time of the blink with this
                                // parameter
        anim.setStartOffset(20);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        txtView.startAnimation(anim);
    }

this function set to blink your textview .....

I hope its useful to you..

The Selection class could be used to achieve your objective. See the official documentation of Selection class below:

http://developer.android.com/reference/android/text/Selection.html

Also, the below link could be useful:

ClickableSpan TextView stays selected after click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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