问题
Kinda like hangman
how to skip over the imageview if something is in it and check to see if something is in it trying to check the imageView if there is an image in it
CODE
final boolean imageThere = mImageView17.setBackgroundResource(null);
public void image6(){
randomNumber();
final int thisLetter = currentLetter;
final boolean imageThere = mImageView17.setBackgroundResource(null);
mImageView6 = (ImageView) findViewById(R.id.imageView6);
mImageView6.setImageResource(thisLetter);
mImageView6.bringToFront();
mImageView17 = (ImageView)findViewById(R.id.imageView17);
mImageView6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (imageThere == false) {
mImageView17.setImageResource(thisLetter);
mImageView17.bringToFront();
mImageView6.setImageResource(R.drawable.whitespace);
} else {
mImageView18.setImageResource(thisLetter);
mImageView18.bringToFront();
mImageView6.setImageResource(R.drawable.whitespace);
}
}
});
mImageView17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mImageView6.setImageResource(thisLetter);
mImageView6.bringToFront();
mImageView17.setImageResource(R.drawable.whitespace);
}
});
}
Basically I want to check the ImageView to see if the image in imageview 17 is there, If it is, Skip over 17 and add imageresource 6 to imageview18
I also want to make sure that if I do this for another imageview17 does not get messed up when I add the same code to imageview 6 of 7 or 8 when I click on image view 17
How do I go about fixing this?
回答1:
if (mImageView17.getDrawable() != null) {
// I already have a bitmap
}
the doc for getDrawable
says:
Return the view's drawable, or null if no drawable has been assigned.
回答2:
I think you'd be better off storing your game state in some other Variables / Objects that are designed specifically to store that information, rather than relying on your UI to store the gamestate for you.
So for instance if the user gets 18 guesses you could store an int that represents how many times the user has guessed. And then based on that int you will make a decision on how to update the UI(which image view to set the image in). I don't know the exact mechanics of your game, so I may have overspimplified it, but the overall approach of storing your game state in some Object should be possible no matter what your game mechanics are.
This way you always have access to your game state anything you may need it for, and you can use that int to update the UI accordingly as play continues.
回答3:
You can use getDrawable()
for it to check whether the drawable has been set already. Check for every imageview
and continue looping. Source.
来源:https://stackoverflow.com/questions/16445040/how-to-check-if-an-imageview-has-an-image-in-it-and-skip-over-the-image-to-the-n