android change image onclick imageviev

后端 未结 2 1020
名媛妹妹
名媛妹妹 2021-01-28 12:13

How can I change Image of an Imageview?

I want to get the image associated; if it is img1 I want set the image to img2, if it is <

2条回答
  •  温柔的废话
    2021-01-28 12:36

    first set the tag of imageview in xml to 1

    final ImageView imageview = (ImageView) findViewById(R.id.imageView1);
    
        imageview.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(imageview.getTag().toString()) == 1) {
                    imageview.setBackgroundResource(R.drawable.image2);
    
                    imageview.setTag(2);
    
                } else {
                    imageview.setBackgroundResource(R.drawable.image1);
                    imageview.setTag(1);
    
                }
    
            }
    });
    

提交回复
热议问题