android代码使用for循环多次单击按钮

时间:2015-12-11 09:49:18

标签: android onclick buttonclick

任何1个例子都可以让我举例说明当用户点击一次时多次执行onClick on按钮。当我点击按钮1次时,应该在延迟5秒后自动点击100次。如何执行。 这是我的示例代码

mUnlock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //It should be already ensured that this mSelectedLock is something user is authorized to access
                    if (mSelectedLock.unlock("RANDOM")) {
                        mUnlock.setVisibility(View.INVISIBLE);
                        mUnlock.postDelayed(new Runnable() {
                            public void run() {
                                mUnlock.setVisibility(View.VISIBLE);

                            }
                        }, 5000);


                    } else {
                        Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
                    }
                }
            });

2 个答案:

答案 0 :(得分:1)

@覆盖     public void onClick(查看v){actionToBeDone(); startLoop(0);}

private void startLoop(final int i) {
    if(i!=100) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("i",""+i);
                actionToBeDone();
                startLoop(i+1);
            }
        }, 2000);
    }
}

private void actionToBeDone() {
    //enter actions you want to be done
    Log.e("actionToBeDone","Button Action");
}

答案 1 :(得分:0)

int count = 0;    
Timer timer = new Timer();
timer.schedule(new TimerTask(){
   @Override
   public void run(){
      if(count < 100){
          mUnlock.performClick();
      }
   }
}, 0, 5000);

5000是你可以+/-从这里开始的毫秒时间。