计时器onClick Android

时间:2017-02-03 08:24:42

标签: java android timer

我试图做一些隐藏的菜单实现。像Android OS中的开发者选项,你按10次,它会打开一个秘密菜单。

我的任务是创建一个onbutton点击监听器来触发某种计时器事件。我必须单击该按钮10次,然后会出现秘密菜单。更重要的是它应该有8秒重置计时器,如果用户在8秒内没有按下按钮,则会重置点击次数。我理解它应该在工作线程上完成,但我在找到这种工作的最佳实践和类时遇到了问题。

5 个答案:

答案 0 :(得分:2)

你不需要在这里实际实现计时器,只需记下用户点击按钮时的时间,并在每次比较8秒时比较差异,否则重置。

假设您有一些方法,例如manageAction()

long lastHitTime;
int counter;
boolean manageAction() {
    if (System.currentTimeMillis() - lastHitTime <= 8000) {
        counter++;
        return counter>=10;

    }
    counter=0;
    return false;
}

答案 1 :(得分:2)

你可以试试这个,

public class MainActivity extends AppCompatActivity {

    private int clickCount = 0;
    private int maxClick = 10;
    private Toast mToast;
    private long clickDelayTime = 500; // milli second (if you want to set 8 sec then set 8000 instead of 500)
    private CountDownTimer mCountDownTimer = new CountDownTimer(clickDelayTime, clickDelayTime) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            // set click count to 0 if user stop clicking before timer finish
            clickCount = 0;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TextView tvClick = (TextView) findViewById(R.id.tvClick);
        tvClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // increment click count
                clickCount++;
                // cancel previous timer
                mCountDownTimer.cancel();
                // start new timer after next click
                mCountDownTimer.start();

                if (clickCount == maxClick) {
                    // cancel current toast
                    mToast.cancel();
                    Toast.makeText(MainActivity.this, "You have clicked " + clickCount + " times.", Toast.LENGTH_SHORT).show();
                    clickCount = 0;
                    return;
                }

                // create toast is null
                if (mToast == null) {
                    mToast = Toast.makeText(MainActivity.this, "Click " + (maxClick - clickCount) + " more times.", Toast.LENGTH_SHORT);
                } else {
                    // if toast is not null then update message
                    mToast.setText("Click " + (maxClick - clickCount) + " more times.");
                }
                mToast.show();
            }
        });
    }
}

答案 2 :(得分:0)

我做过类似你的任务,所以试试吧。

首先声明两个参数,一个计算你的命中时间,另一个指示是否应该重置命中时间。

int hitTimes = 0;
boolean isTheFirstHit = true;

然后,添加侦听器并创建计算命中时间的方法。

    YOUR_BUTTON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSecretMenu();
            countTimes();
        }
    });

private void countTimes(){

    if (isTheFirstHit){

        Log.d("YOUR TAG", "hitTimes = " + hitTimes);
        isTheFirstHit = false;

        new Thread(){
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                Log.d("YOUR TAG", "start time: " + start);

                while (true){
                    SystemClock.sleep(500);
                    long end = System.currentTimeMillis();
                    if (end - start > 8000 ){
                        isTheFirstHit = true;
                        clickTimes = 0;
                        Log.d("YOUR TAG", "reset hitTimes after 8 seconds");
                        break;
                    }
                }
            }
        }.start();
    }
}

然后,当你经常按下按钮8次时显示你的秘密菜单

private void showSecretMenu(){

    hitTimes++;
    Log.d("YOUR TAG", "current hit times = " + hitTimes);

    if (hitTimes == 8 && !isTheFirstHit){
        // SHOW YOUR SECRET MENU HERE
    }
}

答案 3 :(得分:0)

试试这个

INSERT INTO ScholarshipDiploma (Scholarship_id, course) values (1,1), (1,3), (1,6)

答案 4 :(得分:0)

这是我与你的帮助人员一起管理的解决方案。

private long startTime;
private boolean isClicked = false;
private short clickCount;     

tvPhoneTag.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!isClicked) {
                startTime = System.currentTimeMillis();
                isClicked = true;
                clickCount++;
            }
            if (System.currentTimeMillis() - startTime <= 10000) {
                clickCount++;
                if (clickCount == 8) {
                    //DO SOMETHING
                    clickCount = 0;
                }

            } else {
                clickCount = 0;
                isClicked = false;
            }
        }
    });