Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

android - How to change the background color of a TableRow when focused?

I have a table row and I tried many things to change its color when it's focused, but it never changes color when focused. Is there any way to do this? I tried this also which changed color on click but only when its' focused:

r.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View arg0, MotionEvent arg1) {
        if(arg0.isFocused())
        {
            arg0.setBackgroundColor(Color.BLUE);
            //arg0.isFocused();
        }
        return false;
}});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to set the background color of your row to a state list drawable (that handles select, pressed, active, non-active).

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

    <?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active state --> 
    <item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />     
<!--  Inactive state--> 
    <item android:state_selected="false" android:state_focused="false"         android:state_pressed="false" android:drawable="@android:color/transparent" />
     <!--  Pressed state-->
     <item android:state_pressed="true" android:drawable="@android:color/yellow" /> 
    <!--  Selected state (using d-pad) -->
     <item android:state_focused="true" android:state_selected="true"         android:state_pressed="false" android:drawable="@android:color/yellow" />
 </selector> 

try these links too, for ur problem

http://www.gersic.com/blog.php?id=56

http://developer.android.com/guide/topics/ui/themes.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...