Android Studio编程第二篇:电机筛查任务(MOT)
目标
彩色的十字架出现在屏幕上的不同位置,一次一个。参与者必须尽可能快速和准确地选择屏幕上的十字架。
代码
MainActivity
package com.example.mot;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
Integer randomX = 0;
Integer randomY = 0;
Integer screenWidth = 0;
Integer screenHeight = 0;
Integer round = 0;
long startTime = 0;
long endTime = 0;
long allTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getScreen();
genRandomButton();
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
long elapseTime = endTime - startTime;
allTime = allTime + elapseTime;
round++;
TextView textView = findViewById(R.id.textView);
textView.setText("第" + round + "轮用时:" + elapseTime + "毫秒\n" + "总共用时:" + allTime + "毫秒");
try {
TimeUnit.MILLISECONDS.sleep(genRandomTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
genRandomButton();
}
});
}
public void getScreen() {
Resources resources = this.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
public void genRandomButton(){
Random rand = new Random();
randomX = rand.nextInt(screenWidth - 120) + 60;
randomY = rand.nextInt(screenWidth - 120) + 60;
Button button = findViewById(R.id.button);
button.setX(randomX);
button.setY(randomY);
button.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
}
public Integer genRandomTime(){
Random rand = new Random();
Integer randomTime = rand.nextInt(3000) + 200;
return randomTime;
}
}
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_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ring"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.335"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="visible"
app:layout_constraintVertical_bias="0.62" />
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="50dp"
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.022" />
</androidx.constraintlayout.widget.ConstraintLayout>
效果
原文地址:http://www.cnblogs.com/z5onk0/p/16852612.html
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。