Android的按钮setColorFilter行为 [英] Android button setColorFilter behaviour

查看:157
本文介绍了Android的按钮setColorFilter行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我改的问题了一下,因为这个问题是一个有点清晰了)

我有我的应用程序4个按钮,当用户clickes某个按钮 我改变该按钮的颜色。

当按钮3被点击我要改变自己的颜色为绿色,否则我想删除他的绿色过滤器(当按钮1/2/4被点击)。 如果我按一下按钮3它得到的绿色过滤器。如果然后我点击按钮,4它消除了绿色过滤器,但如果我点击按钮1或2,没有任何反应。 当我换了XML按钮的位置,把按钮3第一,它没有发生,想法?

布局XML的相关部分是:

 <按钮
机器人:ID =@ + ID / ANS1
机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT/>
<按钮
机器人:ID =@ + ID / ANS2
机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT/>
<按钮
机器人:ID =@ + ID / ANS3
机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT/>
<按钮
机器人:ID =@ + ID / ANS4
机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT/>
 

在code是:

 如果(answer.equals(3))
    {
        question.setText技术(如果);
        d.setColorFilter(过滤器);
    }
    其他
    {
        question.setText(其他);
        d.setColorFilter(空);
    }
 

解决方案

我好像记得以前创建过多ColorFilters时有问题。这听起来并不为某些像那什么是错在这里,因为它发生的时候了。不过,你可以尝试是具有过滤器类变量,然后在if / else块中使用它。另外,崔佛提到的,因为你只是想删除绿色过滤器,你可以传递null setColorFilter并避免使透明的过滤器,所以你最终的东西是这样的:

  //主类
PorterDuffColorFilter greenFilter =
    新PorterDuffColorFilter(Color.GREEN,PorterDuff.Mode.SRC_ATOP);

//在CheckAnswer()
绘制对象D = findViewById(R.id.ans2).getBackground();

如果(answer.equals(1)d.setColorFilter(greenFilter)
否则d.setColorFilter(空);
 

(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);

这篇关于Android的按钮setColorFilter行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆