我正在为 android 开发一个游戏,我将在其中使用随机图像。我的问题是,只要该图像视图中有某个图像(例如,在 中随机生成的 Image1 id ImageView
),我希望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]);
}
}