嗨所以我目前正在搞乱android / Java代码。我正在处理的小程序随机选择并显示图像。这很好用,但现在我要做的是将某些图像链接到一个按钮。
例如,如果从绘图中显示猫的图片,则按下猫按钮然后将选择下一个随机图像,并且如果狗的图像然后狗按钮将是唯一正确的输入那么明智。如果按下了错误的按钮,程序将通过屏幕进入游戏。
目前我已经设置了代码,所以如果显示就开始随机图像,按下按钮无论图像是什么,它都会加载下一个。这是我到目前为止使用的部分代码:
image = (ImageView) findViewById(R.id.Hold_Image);
Random rand = new Random();
int rndN = rand.nextInt(16)+ 1 ;
String imgName = "img" + rndN;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
image.setImageResource(id); //creating the random selector
cat = (Button) findViewById(R.id.Cat);
dog = (Button) findViewById(R.id.Dog);//button setup
setButtonOnClickListeners();
}
private void setButtonOnClickListeners(){//button code
cat.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(???){//what should call the image number?
Random rand = new Random();
int rndN = rand.nextInt(16)+ 1 ;
String imgName = "img" + rndN;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); }//next random image upon correct button
Word.setImageResource(id);
else{
//game over screen code(yet to write)
}
}
});
Update1:这是我尝试使用Vlads方法的代码。我遇到的问题是按钮没有做任何事情,随机图像也不起作用(每次只显示一张相同的图像)
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity1 extends Activity {
ImageView Word;
Button cat;
Button dog;
int max = 16;
int score = 0;
String imgName;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random rand = new Random();
setContentView(R.layout.activity_main_activity1);
Word = (ImageView) findViewById(R.id.colour);
cat = (Button) findViewById(R.id.theCat);
dog = (Button) findViewById(R.id.theDog);
int rndN = rand.nextInt(16)+ 1 ;
String imgName = "cat" + rndN;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
if (id == 0) {
imgName = "dog" + rndN;
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
Word.setImageResource(id);
}
}
private void nextImage() {
int rndN = rand.nextInt(16)+ 1 ;
String imgName = "cat" + rndN;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
if (id == 0) {
imgName = "dog" + rndN;
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
}
setButtonOnClickListeners();
Word.setImageResource(id);
}
private void setButtonOnClickListeners(){
cat= (Button) findViewById(R.id.theCat);
cat.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View buttonClicked) {
if(imgName.startsWith("dog") && buttonClicked.getId() == R.id.theCat ||
imgName.startsWith("cat") && buttonClicked.getId() == R.id.theDog) {
nextImage();
} else {
//game over screen code(yet to write)
}
}
});}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
首先,给不同类型的图像添加不同的名称,例如所有狗图片dog1,dog2,...以及所有猫图片cat3,cat4,... 确保数字保持不变即可。然后将imgName定义为类变量并添加此方法:替换行" String imgName =" img" + rndN;"有了这个:
private void nextImage() {
Random rand = new Random();
int rndN = rand.nextInt(16)+ 1 ;
String imgName = "dog" + rndN;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
if (id == 0) {
imgName = "cat" + rndN;
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
}
image.setImageResource(id);
}
现在用这个替换你的onClick方法:
@Override
public void onClick(View v) {
if(imgName.startsWith("dog") && v.getId() == R.id.Dog ||
imgName.startsWith("cat") && v.getId() == R.id.Cat) {
nextImage();
} else {
//game over screen code(yet to write)
}
}
而且,显然,也替换了" image = ..."之间的部分。和" cat = ..." with nextImage()。