ImageView onClickListener changing the image source

随声附和 提交于 2019-12-25 06:49:45

问题


Im working on changing the image being shown when I have my ImageView Clicked. Im trying to use a similar code that I used for accomplishing this with a TextView but I can't seem to find the right terms to get it to work. Here is my current code. Thanks

electronconfiguration.setOnClickListener(new View.OnClickListener() {

    public void onClick(View drawable) {

        if (drawable.equals(R.drawable.element_el))
        electronconfiguration.setImageDrawable(R.drawable.aluminum_el);

        else if (drawable.equals(R.drawable.aluminum_el))
        electronconfiguration.setImageDrawable(R.drawable.element_el);
    }
});

回答1:


Why don't you use a ViewSwitcher, it's designed to switch between two views




回答2:


drawable probably doesn't equal R.drawable.element_el. R.drawable.element_el is probably some random implementation of the image. Try drawable.getId().equals(R.drawable.element_el). I've never tried this so I have no idea



来源:https://stackoverflow.com/questions/3497157/imageview-onclicklistener-changing-the-image-source

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