android set button background programmatically

前端 未结 8 732
天涯浪人
天涯浪人 2020-11-30 02:57

I would like to know how to set the button color programatically? I have coded the following but fails:

Button11.setBackgroundColor(R.color.red);
         


        
8条回答
  •  旧巷少年郎
    2020-11-30 03:23

    Using setBackgroundColor() affects the style. So, declare a new style of the same properties with respect to the previous button, with a a different color.

    
    
    
    
    
    

    Now, use OnClick method.

    location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                location.setBackgroundResource(R.drawable.green);
    
            }
        });
    

    this changes the button but looks similar to changing the background.

提交回复
热议问题