问题
I have 16 imageview, each set to onBtnClick as a listner, inside this method it checks whether the imageview id corresponds to a specific id, I need to change this so it checks the image resource inside the imageview, e.g.
public void onBtnClicked(View v)
{
if( v.getId() == R.id.img1 )
{
Intent guess = new Intent(this, com.Logo_Master.Guesslogo.class);
guess.putExtra("image","img1");
startActivity(guess);
}
}
needs to be something like:
public void onBtnClicked(View v)
{
if( v.getId() == R.drawable.img1 )
{
Intent guess = new Intent(this, com.Logo_Master.Guesslogo.class);
guess.putExtra("image","img1");
startActivity(guess);
}
}
or something similar.....so it checks the image inside the imageview rather than the imageview......
Thank you.
/EDIT/
Random random = new Random( System.currentTimeMillis() );
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < imageViews.length; i++) {
int v = imageViews[i];
int next = random.nextInt( 16 );
if ( !generated.contains( next ) ) {
generated.add( next );
ImageView iv = (ImageView) findViewById( v );
iv.setImageResource( images[next] );
Object tag = (Object) new Integer(getImageName(this, String.valueOf(images[next])));
iv.setTag(tag);
}
else {
i--;
}
}
public static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("id/" + imageName + "guess", null, context.getPackageName());
}
public static int getImageName(Context context, String imageName)
{
return context.getResources().getIdentifier("drawable/" + imageName + "guess", null, context.getPackageName());
}
回答1:
You can use tags to achieve this:
When creating your ImageView (in code or in XML), define a tag:
android:tag = "tag"
OR
Object tag = (Object) new Integer(R.drawable.img1);
imageView.setTag(tag);
Then retrieve the tag before you need it by:
Object tag = imageView.getTag();
int drawableId = Integer.parseInt(tag.toString());
if( drawableId == R.drawable.img1 ) {
....
}
Good luck!
回答2:
There is no way to do this directly with an imageView. There is however nothing stopping you from subclassing imageView and adding the required functionality. For example:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;
public class MyImageButton extends ImageButton {
private static final String ANDROID_NAME_SPACE = "http://schemas.android.com/apk/res/android";
private int mDrawableResId = -1;
public MyImageButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
mDrawableResId = attrs.getAttributeResourceValue(ANDROID_NAME_SPACE, "src", -1);
}
public MyImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDrawableResId = attrs.getAttributeResourceValue(ANDROID_NAME_SPACE, "src", -1);
}
@Override
public void setImageResource(int resId){
super.setImageResource(resId);
mDrawableResId = resId;
}
public int getDrawableResId(){
return mDrawableResId;
}
}
This catches setting the drawable directly and from XML.
(diclaimer: I have never tried this however it should do exactly what you want. The example above does work in the emulator. If anyone has any opinions on this approach I would love to hear them.)
来源:https://stackoverflow.com/questions/10886304/checking-imageview-resource-on-actionlistener-rather-than-the-id-of-the-imagevie