1 Public classMainactivityextendsActivityImplementsonclicklistener{2 3 Privatebutton button;4 PrivateEditText EditText;5 PrivateImageView ImageView;6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Tenbutton =(Button) Findviewbyid (R.id.button); OneButton.setonclicklistener ( This); AEditText =(EditText) Findviewbyid (r.id.edit_text); -ImageView =(ImageView) Findviewbyid (R.id.image_view); - } the - @Override - Public voidOnClick (View v) { - Switch(V.getid ()) { + CaseR.id.button: -Toast.maketext (mainactivity. This, Edittext.gettext (). toString (), Toast.length_long). Show (); + if(/*How to decide the image?*/) A Imageview.setimageresource (R.DRAWABLE.WA2); at ElseImageview.setimageresource (R.drawable.ic_launcher); Break; - } -}
View Code
Purpose: Click the button to switch from one image to another
Question: How do I know which picture is in ImageView at this point?
Try:
1.
if (Imageview.getid () ==r.drawable.ic_launcher) ...
Unable to switch
2. (Idea from http://ask.csdn.net/questions/2010)
if (((ImageView) v). GetId () = = R.drawable.ic_launcher)
The program blew up ...
3. (Idea from http://www.eoeandroid.com/thread-66050-1-1.html?_dsign=afedfe3c 3#)
if (Imageview.getdrawable (). Getconstantstate (). Equals (Getresources (). getdrawable (R.drawable.ic_launcher). Getconstantstate ()))
Available
Principle: Drawable.constantstate Abstract class holds information about shared resources of different drawable objects, that is, picture information rather than different object information
4. (Idea from Http://stackoverflow.com/questions/4526585/get-the-id-of-a-drawable-in-imageview)
if ((Integer) ((ImageView) v). Gettag ()). Equals (R.drawable.ic_launcher))
It's fried again.
View Log:android.widget.Button cannot is cast to Android.widget.ImageView
Read it carefully and find that the original text is clicked ImageView instead of button ...
Modify the code as follows
1 @Override2 Public voidOnClick (View v) {3 Switch(V.getid ()) {4 CaseR.id.image_view:5Toast.maketext (mainactivity. This, Edittext.gettext (). toString (), Toast.length_long). Show ();6ImageView IV =(ImageView) v;7Integer integer =Iv.getid ();8Integer = integer==NULL? 0: integer;9 if(integer = = r.drawable.wa2| | Integer = = 0){Ten Imageview.setimageresource (R.DRAWABLE.WA2); One Imageview.settag (R.DRAWABLE.WA2); A } - Else { - Imageview.setimageresource (r.drawable.ic_launcher); the Imageview.settag (r.drawable.ic_launcher); -} Break; - } -}
Wrong Code
Click on the picture no longer explode, but the picture will not change. Change the If content once again
Log out of the picture information and imageview information, found all different ...
However, the discovery of interfaces is so defined ... It's just like ghosts.
Read it carefully again, in fact the code core is Gettag () and Settag () ... I got it all wrong.
The final revision code is as follows:
Imageview.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {toast.maketext (mainactivity. This, Edittext.gettext (). toString (), Toast.length_long). Show (); ImageView IV=(ImageView) v; Integer integer=(Integer) Iv.gettag (); Integer= integer==NULL? 0: integer; LOG.D ("Intval", integer.tostring ()); LOG.D ("Wa2val", ((Integer) r.drawable.wa2). toString ()); LOG.D ("Icval", ((Integer) r.drawable.ic_launcher). toString ()); if(Integer = =r.drawable.wa2) {Iv.setimageresource (r.drawable.ic_launcher); Iv.settag (R.drawable.ic_launcher); } Else{iv.setimageresource (R.DRAWABLE.WA2); Iv.settag (R.DRAWABLE.WA2); } } }); }
Click the button and the picture is working properly
Summary: Two ideas, both get invariants 1) get the invariant of the picture itself: Use Drawable.constantstate 2) to set the invariants manually: set tag for ImageView
How do I get information about the current content in ImageView and compare it?