2012-06-14 39 views
0

我正在開發一個android遊戲,我將在其中使用隨機圖像。我的問題是,當圖像視圖中存在特定圖像時(例如,在ImageView中隨機生成的Image1 id),我希望TextView顯示一條消息,說明Image1可以在圖像視圖中看到。我知道這可以使用條件,如if imageview=imageview1, textview1.settext "blah blah blah"如果條件在Android ImageView中使用

我們該怎麼做。如果我的問題不清楚,請詢問更多細節,請求!!!

package com.random.image; 

import java.util.Random; 



import android.app.Activity; 

import android.app.AlertDialog; 

import android.content.DialogInterface; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.Button; 

import android.widget.ImageView; 

import android.widget.TextView; 


public class RandomImageActivity extends Activity { 

    /** Called when the activity is first created. */ 





private static final Random rgenerator = new Random(); 

private ImageView iv; 

private static final Integer[] mImageIds = 
    { R.drawable.tailss, R.drawable.headss, }; 

@Override 
public void onCreate(Bundle savedInstanceState) 

{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    final TextView t=(TextView) findViewById(R.id.textView1); 

    Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)]; 
    iv = (ImageView) findViewById(R.id.imageView1); 
    changeImageResource(); 

    final Button b=(Button) findViewById(R.id.button1); 
    b.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      AlertDialog a=new AlertDialog.Builder(RandomImageActivity.this).create(); 
      a.setTitle("Flip the Coin"); 
      a.setMessage("Click the button below to find which side of the coin you got"); 
      a.setButton("Try Your Luck", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // here you can add functions 

       changeImageResource(); 





       } 
      }); 
     a.setIcon(R.drawable.coin); 
     a.show(); 


     } 
    }); 
} 

public void changeImageResource() 
{ 

    int i = rgenerator.nextInt(2); 
    iv.setImageResource(mImageIds[i]); 

} 
} 

回答

1

只需使用setTag並獲取即可在ImageView上使用Tag方法,並從中取消條件表達式。不需要額外的數據結構。

例子。

imageView.setImageResource(mImageIds[i]); 
imageView.setTag(mImageIds[i]); 

然後做

if(imageView.getTag().equals(String.valueOf(mImageIds[i])) 
{ 
    textView.setText("blabla"); 
} 

甚至更​​好

imageView.setImageResource(mImageIds[i]); 
imageView.setTag(mImageIds[i]); 

然後

switch(Integer.valueOf(imageView.getTag()){ 
    case R.drawable.id1: textView.setText("bla1"); break; 
    case R.drawable.id2: textView.setText("bla2"); break; 
} 
+0

你可以舉個例子嗎? –

+0

增加的例子.. – Jug6ernaut

+0

謝謝Jug6ernaut,這真的幫了很多,現在工作正常..........大聲笑 –

1

我假設你是從已知的圖像列表中隨機選擇一個圖像。您可以代替定義的數據結構組,標題字符串的ID的圖片ID:

ImageData { 
    int imageId; 
    int captionId; 
} 

然後創建這些對象的數組(而不是圖像的id int數組),當你選擇一個在隨機,使用imageId字段加載圖像和captionId加載字幕字符串。

編輯

你的代碼有這樣的:

private static final Integer[] mImageIds = 
    { R.drawable.tailss, R.drawable.headss, }; 

我的建議是:

private static final class ImageData { 
    ImageData(int imageId, int captionId) { 
     this.imageId = imageId; 
     this.captionId = captionId; 
    } 
    int imageId; 
    int captionId; 
} 

private static final ImageData[] mImageData = { 
    new ImageData(R.drawable.tailss, R.string.tailss), 
    new ImageData(R.drawable.headss, R.string headss) 
}; 

然後,在你的代碼,你會做到這一點:

public void changeImageResource() 
{ 
    int i = rgenerator.nextInt(mImageData.length); 
    ImageData data = mImageData[i]; 
    iv.setImageResource(data.imageId); 
    t.setText(getString(data.captionId)); 
} 
+0

對不起特德,現在我所提供的代碼,你可以幫我通過查看代碼? –

+0

@ZaiKhan - 我更新了我的答案。 –

+0

特德甚至你的解決方案運行良好,所以現在很高興我有兩個更好的解決方案,謝謝隊友,歡呼聲。 –