简体   繁体   English

知道使用ImageButtons数组按下了哪个

[英]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.

相关问题 如何判断按下哪个SHIFT键? - How to tell which SHIFT key was pressed? 使用imageButtons切换活动。 如何在按下按钮的第二个活动中切换图像? - Using imageButtons to switch activities. How can I switch image in second activity based on button pressed? ImageButtons在按下后不会更改图像-LibGDX - ImageButtons wont change Image after being pressed - LibGDX 如何使用 MouseEvent 定义按下哪个按钮 - How to define which button is pressed using MouseEvent 如何判断两个JFrame上的2个按钮阵列中正在按下的按钮 - How to tell what button is being pressed from 2 array's of buttons on two JFrames 在数组中创建ImageButton时是否需要为Context明确设置上下文? - Does Context need to be explicitly set for a ImageButtons when they are created in an array? 确定按下了哪个按钮 - Identifying which button was pressed 有没有办法告诉JVM当前使用哪种GC算法 - Is there a way to tell which GC algorithm the JVM is currently using Java:如何判断我的系统当前使用的是哪个fontconfig文件? - Java: How to tell which fontconfig file my system is currently using? 如何判断我的Android应用程序正在使用哪些库? - How to tell which libraries my android application is using?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM