简体   繁体   中英

How to make a “clickable” row in Android and highlight the row and texts in TextView

I've created a class called ClickableRow which highlights the row and changes the text color of all the TextViews inside of it, then returns the row to its initial settings after the user stops clicking or scrolls too far, but I feel like android probably already has a built in function for this.

The main thing that I want to accomplish is to change all the TextViews in the row so that they are white when a click happens, and return to their previous color after a click or a scroll happens. I am sort-of accomplishing this, but for now I just have it turning the TextViews text to black afterwards. This isn't always the correct color, so is there a way to highlight text on a click and put it back to its default after?

Thanks.

There's a handy property of all Views in android called the Tag (as you might know).

It means you can store whatever you want as data in a particular View.

So in your case, you will store the textview desired color in its TAG, so you can trivially know its state:

    void setTextViewColor(TextView textView, int color) {
          textView.setTag((Integer)color);
          textView.setColor(color);
    }

    void restoreTextViewToItsDefaultColor(TextView textView) {
        Integer myColor=(Integer)textView.getTag();
        textView.setColor(myColor);
    }    

So given a TextView:

 TextView myTextView=new TextView(context);

 setTextViewColor (Color.RED); // sets to red AND stores red in the tag

later on, you manually change to your row color:

myTextView.setColor(Color.BLUE);

then to revert to the original you do

 restoreTextViewToItsDefaultColor(myTextView);

If you manually change

hope it helps !

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