0

我有 16 个 imageview,每个都设置为 onBtnClick 作为侦听器,在此方法中它检查 imageview id 是否对应于特定 id,我需要更改它以便检查 imageview 内的图像资源,例如

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

需要是这样的:

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

或类似的东西......所以它检查图像视图内的图像而不是图像视图......

谢谢你。

/编辑/

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());
}
4

2 回答 2

1

您可以使用标签来实现这一点:

创建 ImageView(在代码或 XML 中)时,定义一个标签:

android:tag = "tag"

或者

Object tag = (Object) new Integer(R.drawable.img1);
imageView.setTag(tag);

然后在需要之前通过以下方式检索标签:

Object tag = imageView.getTag();
int drawableId = Integer.parseInt(tag.toString());
if( drawableId == R.drawable.img1 ) {
      ....
}

祝你好运!

于 2012-06-04T18:41:16.100 回答
1

无法直接使用 imageView 执行此操作。然而,没有什么能阻止您继承 imageView 并添加所需的功能。例如:

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

这捕获了直接和从 XML 设置可绘制对象。

(免责声明:我从未尝试过,但是它应该完全符合您的要求。上面的示例在模拟器中确实有效。如果有人对这种方法有任何意见,我很想听听他们的意见。)

于 2012-06-05T13:38:38.073 回答