【问题标题】:Checking imageview resource on actionlistener rather than the id of the imageview在 actionlistener 上检查 imageview 资源而不是 imageview 的 id
【发布时间】:2012-06-08 19:42:38
【问题描述】:

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

或类似的东西......所以它会检查imageview而不是imageview中的图像......

谢谢。

/编辑/

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

【问题讨论】:

    标签: android image button imageview


    【解决方案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 ) {
          ....
    }
    

    祝你好运!

    【讨论】:

    • 谢谢,看起来不错,但我认为问题在于设置标签......图像不是硬编码的,所以我试图在循环内设置标签.. ...但是使用此代码,单击时图像什么也不做.....有没有办法在我的原始代码中获取图像资源的字符串?
    • 此代码不应影响 ImageView 的事件行为。您可以发布更多代码(声明图像视图的循环)吗?
    • 我已经用循环和两个自定义函数编辑了原始帖子
    • 请在分配之前记录getImageName(this, String.valueOf(images[next])) 的值并在此处发布结果。
    • ok 使用 log.i 方法 -- Log.i("NAME", String.valueOf(getImageName(this, String.valueOf(images[next])))); -- 它输出 NAME 0.......所以很明显问题出在那儿
    【解决方案2】:

    无法直接使用 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 设置可绘制对象。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多