xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:background="@drawable/home" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp"> <TextView android:id="@+id/tv_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="击中数" android:padding="20dp" android:textSize="30dp" android:textStyle="bold"/> <TextView android:id="@+id/tv_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv_show" android:paddingTop="20dp" android:text="0" android:textSize="30dp" android:textStyle="bold"/> <EditText android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:hint="倒计时" android:padding="20dp" android:textSize="30dp" android:textStyle="bold"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp"> <Button android:id="@+id/restart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" android:padding="20dp" android:background="@color/teal_200" android:text="重新开始" android:textSize="20dp" android:textStyle="bold" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:padding="20dp" android:background="@color/teal_200" android:text="开始游戏" android:textSize="20dp" android:textStyle="bold" /> </RelativeLayout> </LinearLayout> <TableLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.8" android:stretchColumns="*" android:shrinkColumns="*"> <TableRow> <ImageView android:id="@+id/img1" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img2" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img3" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img4" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> </TableRow> <TableRow> <ImageView android:id="@+id/img5" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img6" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img7" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img8" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> </TableRow> <TableRow> <ImageView android:id="@+id/img9" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img10" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img11" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> <ImageView android:id="@+id/img12" android:layout_width="90dp" android:layout_height="80dp" android:background="@drawable/dong"/> </TableRow> </TableLayout> </LinearLayout>
Java代码
package com.example.myapplication_dadishu; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_start; private TextView tv_count; private TextView tv_time; private Button btn_restart; private ImageView[] imgs = new ImageView[12]; private int[] ids={R.id.img1,R.id.img2,R.id.img3,R.id.img4,R.id.img5,R.id.img6,R.id.img7,R.id.img8,R.id.img9, R.id.img10,R.id.img11,R.id.img12}; // 游戏是否开始(老鼠能否出洞) private boolean flag=true; // 分数统计 private int count; //原来洞的位置 private int oldIndex=0; //现在洞的位置 private int newIndex=0; //消息处理机制 Handler handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { imgs[oldIndex].setBackgroundResource(R.drawable.dong); // 随机数的范围:0~15 (目的:地鼠下一次出现的位置) newIndex = (int) (Math.random()*12); imgs[newIndex].setBackgroundResource(R.drawable.dishu); oldIndex = newIndex; } }; Runnable runnable = new Runnable() { @Override public void run() { while (flag){ try { Thread.sleep(1000); Message message = Message.obtain(); handler.sendMessage(message); } catch (InterruptedException e) { e.printStackTrace(); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { flag=true; new MyCountDownTimer(10*1000,1000).start(); Thread thread=new Thread(runnable); thread.start(); } }); // 重启游戏 btn_restart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { flag = true; new MyCountDownTimer(10*1000,1000).start(); Thread thread1 = new Thread(runnable); thread1.start(); } }); for (int i = 0; i < imgs.length; i++) { imgs[i].setOnClickListener((View.OnClickListener) this); } } public void init(){ btn_start=findViewById(R.id.start); tv_count=findViewById(R.id.tv_count); tv_time=findViewById(R.id.tv_time); btn_restart=findViewById(R.id.restart); for (int i = 0; i < imgs.length; i++) { imgs[i]=findViewById(ids[i]); } } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_time: new MyCountDownTimer(10*1000,1000).start(); flag=true; Thread thread=new Thread(runnable); thread.start(); break; case R.id.tv_count: flag=true; new MyCountDownTimer(10*1000,1000).start(); Thread thread1=new Thread(runnable); thread1.start(); count=0; break; default: if (imgs[oldIndex].getId()==v.getId()){ count++; tv_count.setText(""+count); } } } private class MyCountDownTimer extends CountDownTimer{ /** * @param millisInFuture The number of millis in the future from the call * to {@link #start()} until the countdown is done and {@link #onFinish()} * is called. * @param countDownInterval The interval along the way to receive * {@link #onTick(long)} callbacks. */ public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } /** * Callback fired on regular interval. * * @param l The amount of time until finished. */ @Override public void onTick(long l) { long h=l/1000/3600; long m=l/1000%3600/60; long s=l/1000%3600%60; tv_time.setText(h+":"+m+":"+s); } /** * Callback fired when the time is up. */ @Override public void onFinish() { flag=false; tv_time.setText("game over"); } } }