how to change the image of a button with every click?

前端 未结 3 2178
情书的邮戳
情书的邮戳 2021-02-09 15:59

I created a button in the layout . In the Drawable folder I created a XML file named btn01_state. The btn0

相关标签:
3条回答
  • 2021-02-09 16:19

    Make it in code perhaps. Put a listener on the button and when the button is clicked the background is changed.

    0 讨论(0)
  • 2021-02-09 16:41

    You can do it easily within the code.

    boolean isPressed = false;
    button.setOnClickListener(buttonListener);
    
    OnClickListener buttonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPressed)
               button.setBackgroundResource(R.drawable.icon1);
            else
               button.setBackgroundResource(R.drawable.icon2);
    
            isPressed = !isPressed;
       }
    };
    
    0 讨论(0)
  • 2021-02-09 16:41

    Simple way

    btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
        btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.locationbutton_on));
                    }
            }); 
    
    0 讨论(0)
提交回复
热议问题