change checkbox background color in android

前端 未结 6 982
一向
一向 2020-12-15 08:31

i have to develop one app.here i have to use checkbox.here i have to select checkbox means the default background color is yellow.but i wish to change the background

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 09:06

    if you are intersted to change the background color of the checkbox (button) use

    mcheckbox.setButtonDrawable(R.drawable.someotherbackground);
    

    where someotherbackground is an image in the drawable folder to which background you want your checkbox to be changed

    try as below

     mcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
    
                if (isChecked) {
    
                    System.out.println("checked" + isChecked);
                    mcheckbox.setButtonDrawable(R.drawable.imageWhenActive);
                        System.out.println("app constant is set as "+isChecked);
                }
                else
                {
                    mcheckbox.setButtonDrawable(R.drawable.imageWheninactive);
                    System.out.println("app constant is set as "+isChecked);
                }
    
            }
        });
    

提交回复
热议问题