之后的onclick切换按钮的颜色 [英] switching color of buttons after onclick

查看:191
本文介绍了之后的onclick切换按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个buttons.i想,当我在任何按钮的颜色应改为和其他按钮单击应该保持same.Next时候,我在其他按钮,单击它的颜色应该是变化等保持不变(或在由defalut状态),等等。
这里是我的code

i have several buttons.i want when i click on any of the buttons its color should be changed and other buttons should remain the same.Next time when i click on other button its color should be change and other remain the same(or in by defalut state),and so on. here is my code

switch(v.getId())
{
case R.id.bt1:
bt11.getBackground().setColorFilter(Color.CYAN,PorterDuff.Mode.Multiple);
bt12.setBackgroundResource(android.R.drawable.btn_default);
break;

case R.id.bt2:
bt12.getBackground().setColorFilter(Color.CYAN,PorterDuff.Mode.Multiple);
bt11.setBackgroundResource(android.R.drawable.btn_default);
break;
}

但是当我pressed按钮BT11第一次它的颜色变成青色,当我preSS BT12那么它的颜色变成青色和第一个按钮即BT11,在其默认状态,但接下来的时间一切是怎么了?这两个按钮保留在青色色

but when i pressed button bt11 very first time its color becomes CYAN and when i press bt12 then its color becomes CYAN and the first button i.e bt11 comes in its default state but next time everything is going wrong both the buttons remain in the CYAN color

推荐答案

试试这个
在code中的问题是你把上的按钮的BG财产的彩色滤光片,它仍将即使你改变了BG,而不是设置过滤器的imgand设置为BG FR BTN

try this the problem in your code is you are putting a color filter on the button's bg property and it will remain even if you change the bg instead set the filter on the imgand set it as bg fr btn

 switch(v.getId())
    {
    case R.id.bt1:
        Drawable d=b11.getBackground();
        d.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);

        b11.setBackgroundDrawable(d);
        b12.setBackgroundResource(android.R.drawable.btn_default);
    break;

    case R.id.bt2:
    //b2.getBackground().setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
    Drawable dd=b12.getBackground();
    dd.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);

    b12.setBackgroundDrawable(dd);
    b11.setBackgroundResource(android.R.drawable.btn_default);
    break;
    }

这篇关于之后的onclick切换按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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