Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...