简体   繁体   English

如何检查代码中按钮的背景(在设计的各种可绘制xml之间),以便可以对其进行修改?

[英]How can I check which background my button has (between various drawable xml i designed) in my code so that i can modify it?

button1.setOnClickListener(new View.OnClickListener() { button1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        if (//button1 background is blue){

            changeBackgroundColorRed();

        }
        else if (//button1 background is red){

            changeBackgroundColorBlue();

        }           
    }

    }); 

The changeBackgroundColor methods are working I just need to know which background they currently have to be able to call the method. changeBackgroundColor方法正在运行,我只需要知道它们当前必须能够调用该背景的背景即可。

You can set the tag of the button depending of what color you have set and get the Tag to check if its currently blue or red. 您可以根据设置的颜色来设置按钮的标签,并获取标签以检查其当前是蓝色还是红色。

In onCreate or similar: 在onCreate或类似的:

When instantiating the Button you need to set the tag first to the initial color: 实例化Button时,您需要首先将标签设置为初始颜色:

button.setTag("blue");

Inside Onclick 内部Onclick

if (button.getTag().equals("blue")){ //background is currently blue
    changeBackgroundColorRed();
    button.setTag("red"); //set the Tag to red cause you change the background to red
}
else if (button.getTag().equals("red")){ //background is currently red
    changeBackgroundColorBlue();
    button.setTag("blue"); //set the Tag to blue cause you change the background to blue
}

Though the accepted answer is correct it has it doesnot seem to be very elegant(my personal opinion). 尽管公认的答案是正确的,但它似乎并不十分优雅(我个人认为)。 Heres my code and it acts as a toggle button. 这是我的代码,它充当切换按钮。 I have set background to android.R.color.black in xm. 我在xm中将背景设置为android.R.color.black。 Changing background requires two steps. 更改背景需要两个步骤。 First get the current background. 首先获取当前背景。 Second compare and change it. 其次比较并更改它。 Something like this should work: 这样的事情应该起作用:

It requires Api level 11. 它要求Api级别11。

public class Sample extends Activity {
Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(OnClickOKButton());
}

View.OnClickListener OnClickOKButton() {
    return new View.OnClickListener() {
        public void onClick(View v) {
            ColorDrawable currentColor = (ColorDrawable) button1
                    .getBackground();
            int color1 = currentColor.getColor();
            if (color1 == getResources().getColor(android.R.color.black)) {
                button1.setBackgroundColor(getResources().getColor(
                        android.R.color.darker_gray));
            } else {
                button1.setBackgroundColor(getResources().getColor(
                        android.R.color.black));
            }
        }
    };
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何修改我的代码,以免死锁? - How can i modify my code so it will be safe from deadlock? 如何读取带有数组的 json 文件,如何修改我的代码以读取具有结果和 HD 的 json - How to read a json file with array, how can i modify my code to read json which has result and HD Android Studio:如何从用于设置按钮背景的可绘制对象中删除白色背景? - Android Studio: How do I remove the white background from my drawable, which is used to set the background of my button? 如何以编程方式将 XML drawable 设置为背景图像? - How can I set an XML drawable as a background image programatically? 我怎样才能检查我的sqlite表中是否有数据? - How can i check to see if my sqlite table has data in it? 如何检查哪些所有用户已登录到我的应用程序 - How can i check which all users are logged into my application 选择ListItem后,如何修改它的背景? - How can I modify the background of a ListItem when it has been selected? 我可以从我的 Java 代码访问和修改 style.xml 吗? - Can I acess and modify styles.xml from my java code? 如何重置 RecyclerViewAdapter 中的可绘制按钮? - How can I reset button drawable in RecyclerViewAdapter? 如何修改我的if语句,以便每隔一秒钟输出一次值? - How can I modify my if statement so that the values print out after every full second?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM