开发者

Android button setColorFilter behaviour

(I changed the question a bit, because the problem is a bit clearer now)

I have 4 buttons on my application, and when a user clickes certain button I change that button color.

when button 3 is clicked I want to change his color to green, otherwise I want remove his green filter (when button1/2/4 are clicked). If I click on button 3 It does get the green filter. If then I click button 4 it removes the green filter, but if I click button 1 or 2, nothing happens. When I switched the position of the buttons in the XML, and put button3 first, It doesnt happen, ideas?

The relevant part of the layout xml is:

<Button
android:id="@+id/ans1"
android:layout_width="fill_parent" 
    开发者_运维百科 android:layout_height="wrap_content"/>
<Button
android:id="@+id/ans2"
android:layout_width="fill_parent" 
     android:layout_height="wrap_content" />
<Button
android:id="@+id/ans3"
android:layout_width="fill_parent" 
     android:layout_height="wrap_content" />
<Button
android:id="@+id/ans4"
android:layout_width="fill_parent" 
     android:layout_height="wrap_content" />

The code is:

if (answer.equals("3")) 
    {
        question.setText("In if");
        d.setColorFilter(filter); 
    }
    else
    {
        question.setText("else");
        d.setColorFilter(null);
    }


I seem to remember having issues when creating too many ColorFilters before. It doesn't sound for certain like that's what's at fault here, since it's happening right away. Still, what you might try is having the filter as a class variable, and then using it within the if/else block. Also, as Trev mentioned, since you're just wanting to remove the green filter, you can just pass null to setColorFilter and avoid making the transparent filter, so you'd end up with something like this:

//in main class
PorterDuffColorFilter greenFilter = 
    new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);

//in CheckAnswer()
Drawable d = findViewById(R.id.ans2).getBackground();

if(answer.equals("1") d.setColorFilter(greenFilter)
else d.setColorFilter(null);


The default behavior when calling setColorFilter(ColorFilter) on a Drawable does not automatically invalidate the Drawable, meaning it will not redraw itself solely as a result of the method call.

Try calling d.invalidateSelf() after setting the ColorFilter.


Yesterday I posted a suggestion to a very similar problem that you asked here:

Android button setColorFilter behaviour

It appears that you have edited the code you originally posted there in order to incorporate the suggestions you were given (without acknowledging the answers) and then posted exactly the same code in this question.


The Drawable documentation regarding setColorFilter(ColorFilter cf) states that 'null' may be passed to remove any existing filters. So, perhaps it could be that once the TRANSPARENT filter has been applied, then your subsequent GREEN filter can't be seen? I don't know enough about .setColorFilter and PorterDuff to know for sure, but it's worth a shot. Perhaps try:

d.setColorFilter(null); 
d.setColorFilter(filter); 

Also you could instead use this method:

setColorFilter(int color, PorterDuff.Mode mode) 


You just need to mutate each drawable before setColorFilter.

Drawable d = findViewById(R.id.ans2).getBackground();

d = d.mutate();
d.setColorFilter
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜