android change image onclick imageviev

戏子无情 提交于 2019-12-02 12:47:16

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);

            }

        }
});

I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as @ρяσѕρєя K mentioned in comment.

There are two version of setTag one which takes object as an argument and other takes key and object as an argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!