Android Imagebutton change Image OnClick

前端 未结 7 1126
心在旅途
心在旅途 2020-12-09 07:43

I just added a new drawable folder under res folder. In the drawable folder, i copied the ic_launcher.png file from

7条回答
  •  眼角桃花
    2020-12-09 08:28

    That is because imgButton is null. Try this instead:

    findViewById(R.id.imgButton).setBackgroundResource(R.drawable.ic_action_search);
    

    or much easier to read:

    imgButton = (Button) findViewById(R.id.imgButton);
    imgButton.setOnClickListener(imgButtonHandler);
    

    then in onClick: imgButton.setBackgroundResource(R.drawable.ic_action_search);

提交回复
热议问题