目标

参与者必须选择并按住屏幕底部的一个按钮。上面有一个圆圈(一个用于简单模式,五个用于五种选择模式)。在每一种情况下,其中一个圆圈中都会出现一个黄色的圆点,参与者必须尽快做出反应,释放屏幕底部的按钮,并选择该圆点出现的圆圈。

代码

MainActivity

package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.sax.StartElementListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.os.Message;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    long downTime = 0;//Button被按下时的时间
    long thisTime = 0;//while每次循环时的时间
    long startTime = 0;
    long endTime = 0;
    long randomTime = 0;
    boolean onBtnTouch = false;//Button是否被按下
    boolean firstEnter = true;//Button是否被按下
    boolean firstClick = true;
    TextView randomCircle;
    Integer randomInt;
    Map<Integer, Integer> mapSmallCircle = new HashMap<Integer, Integer>();
    Map<Integer, Integer> mapBigCircle = new HashMap<Integer, Integer>();

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

        Button startButton = findViewById(R.id.button);

        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                TextView text1 = findViewById(R.id.textView);
                text1.setText(String.valueOf(msg.obj));
                randomCircle.setVisibility(View.VISIBLE);
                startTime = System.currentTimeMillis();
                firstClick = true;
            }
        };
        buildMap();
        initCircle();

        for(Integer buttonID:mapBigCircle.values()){
            findViewById(buttonID).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    if (!firstClick) {
                        return;
                    }
                    firstClick = false;
                    Integer randomID = mapBigCircle.get(randomInt);
                    TextView text1 = findViewById(R.id.textView);
                    Integer idd = v.getId();
                    if (v.getId()==randomID){
                        endTime = System.currentTimeMillis();
                        long elapseTime = endTime - startTime;
                        System.out.println(elapseTime);
                        text1.setText("选择正确,用时: " + String.valueOf(elapseTime) + " 毫秒");
                    }
                    else {
                        text1.setText("选择错误!");
                    }
                    randomCircle.setVisibility(View.INVISIBLE);
                }
            });
        }

        startButton.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                firstEnter = true;
                TextView text1 = findViewById(R.id.textView);
                text1.setText("");
                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    gen_random_time();
                    downTime = System.currentTimeMillis();
                    onBtnTouch = true;
                    Thread t = new Thread(){
                        @Override
                        public void run(){
                            while (onBtnTouch) {
                                thisTime = System.currentTimeMillis();
                                if (thisTime - downTime >= randomTime) {
                                    if (firstEnter) {
                                        gen_random_circle();
                                        Message msg = handler.obtainMessage();
                                        msg.obj = "请迅速点击黄点";
                                        handler.sendMessage(msg);
                                    }
                                    firstEnter = false;
                                    try {
                                        Thread.sleep(200);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    };
                    t.start();
                }
                else {
                    text1.setText("");
                    onBtnTouch = false;
                }
                return false;
            }
        });
    }

    public void gen_random_circle(){
        Random rand = new Random();
        randomInt = rand.nextInt(4) + 1;
        Integer ID = mapSmallCircle.get(randomInt);
        randomCircle = findViewById(ID);
    }

    public void gen_random_time(){
        Random rand = new Random();
        randomTime = rand.nextInt(2000) + 1000;
    }
    public void buildMap(){
        mapSmallCircle.put(1, R.id.textView1);
        mapSmallCircle.put(2, R.id.textView2);
        mapSmallCircle.put(3, R.id.textView3);
        mapSmallCircle.put(4, R.id.textView4);
        mapSmallCircle.put(5, R.id.textView5);

        mapBigCircle.put(1, R.id.button1);
        mapBigCircle.put(2, R.id.button2);
        mapBigCircle.put(3, R.id.button3);
        mapBigCircle.put(4, R.id.button4);
        mapBigCircle.put(5, R.id.button5);
    }

    public void initCircle(){
        for(Integer circleID:mapSmallCircle.values()){
            findViewById(circleID).setVisibility(View.INVISIBLE);
        }
    }

}

xml布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingDefaultResource">

    <TextView
        android:id="@+id/textView"
        android:layout_width="250dp"
        android:layout_height="84dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="150px"
        android:layout_marginBottom="1500px"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="750px"
        android:layout_marginBottom="950px"
        android:background="@drawable/green_circle"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="150px"
        android:layout_marginBottom="500px"
        android:background="@drawable/green_circle"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="630px"
        android:layout_marginBottom="500px"
        android:background="@drawable/green_circle"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="430px"
        android:layout_marginBottom="1200px"
        android:background="@drawable/green_circle"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginRight="80px"
        android:layout_marginBottom="950px"
        android:layout_gravity="bottom|right"
        android:background="@drawable/green_circle" />

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="400px"
        android:layout_marginBottom="800px"
        android:background="@drawable/blue_circle"
        android:text="按住我开始游戏" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="830px"
        android:layout_marginBottom="1000px"
        android:elevation="2dp"
        android:background="@drawable/random_circle" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="130px"
        android:layout_marginBottom="1000px"
        android:elevation="2dp"
        android:background="@drawable/random_circle" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="480px"
        android:layout_marginBottom="1250px"
        android:elevation="2dp"
        android:background="@drawable/random_circle" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="680px"
        android:layout_marginBottom="550px"
        android:elevation="2dp"
        android:background="@drawable/random_circle" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="200px"
        android:layout_marginBottom="550px"
        android:elevation="2dp"
        android:background="@drawable/random_circle" />
</FrameLayout>

效果

![](https://static.sucaidaohang.com/scStatic/1adfff86-a465-4102-ab80-9fb3e60c856e.png)

原文地址:http://www.cnblogs.com/z5onk0/p/16852527.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性