分析
屏幕中央显示了一个白色的盒子,其中从2到9的数字以伪随机的顺序出现,每分钟100位。参与者被要求检测数字的目标序列(例如,246、357、468)。当参与者看到目标序列时,他们必须通过尽快地选择屏幕中心的按钮来做出反应。难度程度随着参与者必须同时观察的一个或三个目标序列而变化。
代码
MainActivity
package com.example.rvp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private Map<Integer, Integer> MyMap = new HashMap<Integer, Integer>();
TextView targetText;
TextView countText;
TextView timeText;
TextView rightText;
Integer winCount = 0;
Integer allCount = 1;
private TextView timerView;
private long baseTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
targetText = findViewById(R.id.textView6);
countText = findViewById(R.id.textView9);
timeText = findViewById(R.id.textView8);
rightText = findViewById(R.id.textView10);
buildMap();
changeTarget();
changeButton();
setButton();
MainActivity.this.baseTimer = SystemClock.elapsedRealtime();
timerView = (TextView) this.findViewById(R.id.textView8);
final Handler startTimehandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (null != timerView) {
timerView.setText((String) msg.obj);
}
}
};
new Timer("计时器").scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
int time = (int)((SystemClock.elapsedRealtime() - MainActivity.this.baseTimer) / 1000);
String hh = new DecimalFormat("00").format(time / 3600);
String mm = new DecimalFormat("00").format(time % 3600 / 60);
String ss = new DecimalFormat("00").format(time % 60);
String timeFormat = new String(hh + ":" + mm + ":" + ss);
Message msg = new Message();
msg.obj = timeFormat;
startTimehandler.sendMessage(msg);
}
}, 0, 1000L);
super.onCreate(savedInstanceState);
for (Integer buttonID: MyMap.values()){
Button button = findViewById(buttonID);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (button.getText() == targetText.getText()){
winCount++;
rightText.setText("回答正确");
}
else{
rightText.setText("回答错误");
}
allCount++;
countText.setText("答对" + winCount + "个\n" + "总共第" + allCount + "个");
changeTarget();
changeButton();
setButton();
}
});
}
}
public void buildMap(){
MyMap.put(1, R.id.button1);
MyMap.put(2, R.id.button2);
MyMap.put(3, R.id.button3);
MyMap.put(4, R.id.button4);
MyMap.put(5, R.id.button5);
MyMap.put(6, R.id.button6);
MyMap.put(7, R.id.button7);
MyMap.put(8, R.id.button8);
MyMap.put(9, R.id.button9);
MyMap.put(10, R.id.button10);
MyMap.put(11, R.id.button11);
MyMap.put(12, R.id.button12);
}
public void changeTarget(){
Random rand = new Random();
Integer randomInt = rand.nextInt(899) + 100;
targetText.setText(String.valueOf(randomInt));
}
public void changeButton(){
for(Integer selectButton: MyMap.values()){
Random rand = new Random();
Integer randomInt = rand.nextInt(899) + 100;
Button button = findViewById(selectButton);
button.setText(String.valueOf(randomInt));
}
}
public void setButton(){
Random rand = new Random();
Integer randomInt = rand.nextInt(8) + 1;
Button buttonID = findViewById(MyMap.get(randomInt));
buttonID.setText(targetText.getText());
}
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button10"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.445" />
<Button
android:id="@+id/button7"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.913" />
<Button
android:id="@+id/button8"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.913" />
<Button
android:id="@+id/button9"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.948"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.913" />
<Button
android:id="@+id/button4"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.756" />
<Button
android:id="@+id/button5"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.756" />
<Button
android:id="@+id/button6"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.948"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.756" />
<Button
android:id="@+id/button1"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<Button
android:id="@+id/button11"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.445" />
<Button
android:id="@+id/button12"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.948"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.445" />
<Button
android:id="@+id/button2"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<Button
android:id="@+id/button3"
android:layout_width="103dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.948"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<TextView
android:id="@+id/textView6"
android:layout_width="118dp"
android:layout_height="48dp"
android:gravity="center"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.645"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.297" />
<TextView
android:id="@+id/textView7"
android:layout_width="92dp"
android:layout_height="43dp"
android:gravity="center"
android:text="目标值:"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.304"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.302" />
<TextView
android:id="@+id/textView8"
android:layout_width="131dp"
android:layout_height="36dp"
android:gravity="center"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.453"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView9"
android:layout_width="132dp"
android:layout_height="65dp"
android:gravity="center"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.052" />
<TextView
android:id="@+id/textView10"
android:layout_width="99dp"
android:layout_height="51dp"
android:textSize="20dp"
android:textColor="#FF0000"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.195" />
</androidx.constraintlayout.widget.ConstraintLayout>
效果
原文地址:http://www.cnblogs.com/z5onk0/p/16852633.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性