我想显示随机图片,然后点击一些图片,并显示错误或正确的图片
private int[] imgId = new int[] { R.drawable.info_mna,
R.drawable.info_mn, R.drawable.panda_mna, R.drawable.panda_mn,
R.drawable.share_mn, R.drawable.share_mna, R.drawable.riset_mn,
R.drawable.game_mn };
用于clicklistener
iv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//not working
if (imgId.length == imgId[1]) {
Toast.makeText(getApplicationContext(), "right",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "wrong",
Toast.LENGTH_LONG).show();
}
}
});
答案 0 :(得分:1)
要从数组中获取随机图像,请执行以下操作:
int id = imgId[new Random().nextInt(imgId.length)];
然后在onClick()
iv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//not working
if (imgId.length == id) {
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_LONG).show();
}
}
});