[英]Tell which is pressed using Array of ImageButtons
I have a basic hangman game, and the buttons to make the guesses are all placed on screen with ImageButtons (I had this working perfectly). 我有一个基本的hang子手游戏,用来猜测的按钮都用ImageButtons放在了屏幕上(我的工作很完美)。
I declare the image button ID's like so: 我声明图像按钮ID如下:
private static final int[] BUTTON_IDS = {
R.id.imageButtonA, R.id.imageButtonB, R.id.imageButtonC, R.id.imageButtonD, R.id.imageButtonE,
R.id.imageButtonF, R.id.imageButtonG, R.id.imageButtonH, R.id.imageButtonI, R.id.imageButtonJ,
R.id.imageButtonK, R.id.imageButtonL, R.id.imageButtonM, R.id.imageButtonN, R.id.imageButtonO,
R.id.imageButtonP, R.id.imageButtonQ, R.id.imageButtonR, R.id.imageButtonS, R.id.imageButtonT,
R.id.imageButtonU, R.id.imageButtonV, R.id.imageButtonW, R.id.imageButtonX, R.id.imageButtonY, R.id.imageButtonZ
};
private List<ImageButton> buttonLetters = new ArrayList<ImageButton>();
Edited to show solution: I register their OnClick by doing: 编辑以显示解决方案:我通过执行以下操作来注册他们的OnClick:
for (int id: BUTTON_IDS) {
final ImageButton buttonLettersUsage = (ImageButton)findViewById(id);
if (buttonLetters != null) {
assert buttonLettersUsage != null;
buttonLettersUsage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HangmanActivity.this, " " + buttonLettersUsage.getId(), Toast.LENGTH_SHORT).show();
switch (buttonLettersUsage.getId()){
case R.id.imageButtonQ:
strGuessed = "Q";
break;
case R.id.imageButtonW:
strGuessed = "W";
break;
case R.id.imageButtonE:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
btnGuessClicked(theIncorrectGuesses, theWord);
buttonLettersUsage.setVisibility(View.INVISIBLE);
}
});
}
}
The problem is now, I've been working on the app some more since implementing this part, and all the buttonLettersUsage.getId()
's have changed. 现在的问题是,自实现此部分以来,我一直在开发应用程序,并且所有
buttonLettersUsage.getId()
都已更改。
My question is: Will they change at any time? 我的问题是:它们会随时更改吗? I've not put any new components onto the activity, why have they changed?
我没有在活动中添加任何新组件,为什么要更改它们? Is there a better way to find out which button has been pressed, and give a different value for
strGuessed
depending on which has been pressed? 有没有更好的方法来找出已按下的按钮,并根据已按下的按钮为
strGuessed
赋予不同的值?
Many thanks. 非常感谢。
Don't use the numerical ids in the switch case use it as follows. 不要在切换情况下使用数字ID,请按以下方式使用它。
switch (buttonLettersUsage.getId()){
case R.id.imageButtonA:
strGuessed = "Q";
break;
case R.id.imageButtonB:
strGuessed = "W";
break;
case R.id.imageButtonC:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
And also don't implement onClickListener like this, use your Activity to implement View.OnClickListener and then set the listener in loop as 并且也不要像这样实现onClickListener,请使用Activity实现View.OnClickListener,然后将循环中的侦听器设置为
buttonLettersUsage.setOnClickListener(this);
Where this is the context of your activity which implements the OnClickListener 这是实现OnClickListener的活动的上下文
YourActivity implements View.OnClickListener{
YourActivity
implements View.OnClickListener{
then in oncreate(){
然后在
oncreate(){
//add is buton // add是buton
View buttonAdd = findViewById(R.id.add);
buttonAdd.setOnClickListener(this);}
then
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
{
}
break;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.