Make an Android button change background on click through XML

后端 未结 5 1635
春和景丽
春和景丽 2020-11-27 02:59

Is there a way to specify an alternative background image/color for a Button in the XML file that is going to be applied onClick, or do I have to do a But

5条回答
  •  执念已碎
    2020-11-27 03:33

    To change image by using code

    public void onClick(View v) {
       if(v == ButtonName) {
         ButtonName.setImageResource(R.drawable.ImageName);
       }
    }
    

    Or, using an XML file:

     
    
       
       
       
    
    

    In OnClick, just add this code:

    ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
    

提交回复
热议问题