自动按钮点击同时发生

时间:2014-07-29 00:20:47

标签: java android button automation delay

我尝试了一百种不同的方法来实现自动按钮点击之间的延迟,包括thread.sleep,Handler.postDelayed等等......可能是我以某种方式错误地使用了它。我最近的尝试是使用简单的布尔切换。似乎无论我如何尝试,所有要自动点击的按钮在延迟后同时发生,INSTEAD在点击之间延迟。 我现在的代码:

设置按钮onClickListener:

    for (int i = 0; i < mDifficulty; i++) {
         ButtonsOCLArray[i].setOnClickListener(new OnClickListener() {

            public void onClick(final View v) {
                animating = true;
                while (animating) {
                animateButtons(v);
                }
            }
        });
    }

按钮动画:

    public static void animateButtons(View v) {
      AlphaAnimation fadeInAnimation = new AlphaAnimation(0F, 1F);
      fadeInAnimation.setDuration(1500);
      fadeInAnimation.setFillAfter(true);
      v.startAnimation(fadeInAnimation);
      animating = false;
    }

最后,从一个单独的类,自动按钮设置:

    public void pushAiButton(final View [] v){
       mWhichButton = (mGameAi.getRandomNumber(MainActivity.mDifficulty)); // get random number for random button to press
       mListOfAiButtonsToPress.add(mWhichButton); // add random number to mLOABTP
       mListOfAiButtonsTemp.addAll(mListOfAiButtonsToPress); // add all elements of mLOABTP to mLOABT
       boolean empty = false;
       while (!empty) { 
         if (empty) {
            break;
         }
         tempArrayIndex = mListOfAiButtonsTemp.get(0); // tempArray given value in first slot of mLOABT
         mListOfAiButtonsTemp.remove(mListOfAiButtonsTemp.get(0)); // first slot of MLOABT removed
         if (mListOfAiButtonsTemp.isEmpty()) {
            // looped through whole list, empty now
            empty = true; 
         } // end if
         v[tempArrayIndex].performClick(); // click button at index *button*[*index*]
       } // end !empty while
     } // end pushAiButton()

任何想法都非常感谢!谢谢!

更新 这让它运作起来:

    mButtonStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            Log.d(TAG, "START BUTTON CLICKED!");
            if (firstRun) {
                mGameAi.setupAiButtons();   
                firstRun = false;
            }
            v.setVisibility(View.INVISIBLE);
            animateButtons(ButtonsOCLArray[mGameAi.getFirstButtonInList()]);
            mGameAi.deleteFirstButtonInList();
            v.postDelayed(new Runnable() {
                public void run() {
                    if (!mGameAi.buttonsListIsEmpty()) {
                        v.performClick();
                    }
                    else {
                        v.setVisibility(View.VISIBLE);
                        firstRun = true;
                    }
                }
            }, 500);
            System.out.println("end of mButtonStart's onclicklistener");
        }
    });

1 个答案:

答案 0 :(得分:0)

您已将其编码,以便它们几乎同时点击。自动按钮设置执行一个“while”循环,遍历所有按钮 - 几乎同时删除它们。

换句话说,迭代按钮的while循环需要暂停(或不排队再次点击),直到动画完成。

这是问题所说的另一种方式;当每个“onClick”出现时,布尔“animateButtons”为真,它们都进入animateButtons方法。

你需要在pushAiButton中有一个等待通话的线程,并等待每个按钮完成动画。