如何实现本机 android 库以在 2x2 网格布局中在一秒钟内检测到触摸?

How to implement native android libraries to detect touch within a second in a 2x2 grid layout?

屏幕元素;

TextView shows game score
Rest of the screen is divided into 4 equal colored rectangle views

目标

Per second any of those 4 rectangle views will change its color to gray
the gray color will remain for 1 whole second 
after 1 second the gray rectangle will revert to its original color and again one random rectangle will turn gray
player needs to tap on the rectangles that are grey during that 1 second, so that there is at least 1 tap per second

条件

if user taps on the rectangle view while gray then score moves up by 1+
if user cannot tap the view within 1 second, game over
if user taps on a view that is not gray, also game over

简而言之,objective就是在一秒内点击灰色矩形。如果错过或点击彩色框,游戏结束。

我试过像下面这样编码,我用字母代替了颜色 并使用 X 而不是 GRAY,

请帮助我理解我做错了什么?

感谢任何帮助。

谢谢!

public class MainActivity extends AppCompatActivity {

    Button ba, bb, bc, bd, startgame;
    TextView score;
    GameCountDownTimer gameCountDownTimer;

    // stores button index, original color
    int greyButton[] = new int[2];
    int clicked = 99;
    boolean firstrun=true;

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


        startgame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameCountDownTimer = new GameCountDownTimer(3000, 1000);
                gameCountDownTimer.start();
            }
        });


    }
    public class GameCountDownTimer extends CountDownTimer {
        public GameCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            int progress = (int) (millisUntilFinished/1000);
            score.setText(String.valueOf(progress));
        }
        @Override
        public void onFinish() {
            if(firstrun) {
                randomize();
                firstrun=false;
            }
            else{
                // only continue if player taps on the correct button
                // or clicked == greyButton[0]
                if(clicked==greyButton[0]){
                    randomize();
                    this.start();
                } else {
                    score.setText("GAME OVER!");
                    this.cancel();
                }
            }
        }
    }

    public void randomize(){

        // if grayButton has a value, revert to original color
        if(greyButton != null){
            revertToOriginalColor(greyButton[0]);
        }
        int nextGrayButton = new Random().nextInt(4);
        setGrayButton(nextGrayButton);

    }

    public void revertToOriginalColor(int index){
    }

    public void setGrayButton(int index){
    }
}

编辑: 已解决

在我的 onCreate() 中添加了以下部分 在对我的 activity class.

实施 View.OnClickListener 之后,将所有 4 个按钮注册到覆盖的 onClick
// initialize the CountDownTimer
        timer = new CountDownTimer(1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //updating the time
            }

            @Override
            public void onFinish() {
                gameOver = true;
                this.cancel();
                simpleAlert(score);
            }
        };

我建议使用 Rxjava。 在您的应用级别 build.gradle 文件

中添加以下依赖项
    implementation "io.reactivex.rxjava2:rxjava:2.1.9"
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
    implementation "com.jakewharton.rxbinding2:rxbinding:2.0.0"

在您的 class.

中添加以下代码
 RxView.clicks(my_grid_layout)
                .map(new Function<Object, Integer>() {
                    @Override
                    public Integer apply(Object o) throws Exception {
                        return 1;
                    }
                })
                .buffer(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new Observer<List<Integer>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(List<Integer> integers) {
                        Log.e(TAG, "onNext: " + integers.size() + " taps received!");

                       int number_of_clicks= integers.size();

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete");
                    }
                });

如果您想限制为仅使用 android 个库

private int count = 0;
private Timer myTimer;


relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               count= count+1;
            }
        });

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
              Log.d("TAG",String.valueOf(count));
              count=0;
            }

        }, 0, 1000);
    }