首页 > 编程语言 >Android Studio编程第一篇:反应时间测试(RTI)

Android Studio编程第一篇:反应时间测试(RTI)

时间:2022-11-02 21:36:00浏览次数:45  
标签:void RTI id public Studio put import Android android

目标

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

代码

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>

效果

标签:void,RTI,id,public,Studio,put,import,Android,android
From: https://www.cnblogs.com/z5onk0/p/16852527.html

相关文章