Make an Android button change background on click through XML

后端 未结 5 1634
春和景丽
春和景丽 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:47

    In the latest version of the SDK, you would use the setBackgroundResource method.

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

提交回复
热议问题