从另一个活动的图像视图中获取图像

时间:2013-12-22 17:14:49

标签: android

if( txt.equals("Apple"))
  {
        Toast.makeText(this, "Times up", Toast.LENGTH_SHORT).show();
        AlertDialog ad = new AlertDialog.Builder(this).create();  
        ad.setCancelable(false); // This blocks the 'BACK' button  
        ad.setMessage("You win");  
        ad.setButton("OK", new DialogInterface.OnClickListener() {  
           @Override  
          public void onClick(DialogInterface dialog, int which) {  
              dialog.dismiss();                      
            }  
        });

       ad.show(); 

  }

我应该写什么来比较Apple文本和imageview的图像

2 个答案:

答案 0 :(得分:1)

我在您的代码中看到的内容,您正在尝试compare two drawables using equals method。我认为这不会起作用。

比较两个drawables,一个简单的黑客就是convert them to bitmaps and check using ==

Drawable fDraw = iv1.getDrawable();
Drawable sDraw = getResources().getDrawable(R.drawable.mario_pinball);

Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();

if(bitmap == bitmap2)
    {
        //Code blcok
    }

编辑:基于评论,如果上述方法不起作用

Bitmap类提供了一种方法sameAs()来比较两个位图 http://developer.android.com/reference/android/graphics/Bitmap.html#sameAs%28android.graphics.Bitmap%29

编辑:

根据OP对该问题的评论,为了将文字与图片进行比较,您可以在图片中setTag()getTag()存储文字值,然后将此值与文字视图'进行比较。 s文本。

答案 1 :(得分:1)

  

我应该写什么来比较Apple文本和imageview的图像

你不会。

相反,每当您更改ImageView时,请维护一些其他数据成员,以反映您在int中添加的内容(例如R.drawable的{​​{1}})。然后,您可以检查其他数据成员是否相等。

相关问题