How to toggle background image on button click?

前端 未结 4 1003
无人及你
无人及你 2021-01-26 04:54

I have this code:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
          


        
4条回答
  •  灰色年华
    2021-01-26 05:40

    How about creating an array of the drawable's IDs and saving an index:

    private final int[] myDrawables = {R.drawable.detailpressed, R.drawable.detailpressed1, ...};
    //...
    button1.setOnClickListener(new OnClickListener() {
        int index = 0;
        @Override
        public void onClick(View v) {
            button1.setBackgroundResource(myDrawables[index++ % myDrawables.length]);
            Chapter_sync.add(chapid);
        }
    }
    

提交回复
热议问题