how to change background image of button when clicked/focused?

后端 未结 8 1077
旧时难觅i
旧时难觅i 2020-11-30 01:00

I want to change the background image of a button when clicked or focused.

This is my code:

Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOn         


        
8条回答
  •  Happy的楠姐
    2020-11-30 01:42

    1. Create a file in drawable play_pause.xml
    
    
    
        
    
        
        
    
    
    1. In xml file add this below code
     
    
    1. In java file add this below code
    iv_play = (ImageView) findViewById(R.id.iv_play);
    iv_play.setSelected(false);
    

    and also add this

    iv_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    iv_play.setSelected(!iv_play.isSelected());
                    if (iv_play.isSelected()) {
                        ((GifDrawable) gif_1.getDrawable()).start();
                        ((GifDrawable) gif_2.getDrawable()).start();
                    } else {
                        iv_play.setSelected(false);
                        ((GifDrawable) gif_1.getDrawable()).stop();
                        ((GifDrawable) gif_2.getDrawable()).stop();
                    }
                }
            });
    

提交回复
热议问题