2013-12-22 78 views
-5
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(); 

    } 

我應該寫什麼蘋果文本與ImageView的從另一個活動

+0

Imagview img = new(Imageview)findviewbyid(R.id.fetchimage); – QuokMoon

+0

我也希望它在IF聲明中使用 – user3127549

+0

@ user3127549使用startActivity獲得結果。但你的帖子我不清楚 – Raghunandan

回答

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()和​​以存儲文本值,然後將此值與textview的文本進行比較。

+0

我不會指望在所有情況下返回相同的「Bitmap」對象。 – CommonsWare

+0

而你的編輯速度會很慢,因爲它必須迭代所有像素以進行比較。 – CommonsWare

+0

@CommonsWare請參閱http://stackoverflow.com/questions/9125229/comparing-two-drawables-in-android和http://stackoverflow.com/questions/6120439/comparing-bitmap-images-in-android/7696320 #7696320 – gaurav5430

1

我應該寫什麼蘋果文本與ImageView的

你不會的圖像進行比較。

相反,保持一定的數據成員反映你放什麼ImageView(例如,在R.drawable值的int)每當變更ImageView。然後,您可以檢查其他數據成員是否相等。

+0

thanx爲您的幫助:) – user3127549

相關問題