我正在为Android开发一款游戏,我将使用随机图像。我的问题是我在该图像视图中存在某个图像时(例如,ImageView
中随机生成的Image1 id)我希望TextView
显示一条消息说,Image1可以在图像视图现在。我知道可以使用if条件来完成,说if imageview=imageview1, textview1.settext "blah blah blah"
。
我们怎么做到这一点。如果我的问题不明确,请询问更多细节,请求!!!
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]);
}
}
答案 0 :(得分: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));
}
答案 1 :(得分:1)
只需在ImageView上使用setTag和getTag方法,然后执行条件语句。无需额外的数据结构。
实施例
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;
}