【Android】MyTool工具界面:秒表计录



MyTool工具界面:秒表计录

♦ 回顾

  • 之前我们通过设计完成了工具界面的秒表,今天在此基础上进行优化,将实现对时间的记录。
    在这里插入图片描述

    需求:
      1. 3个textview
      2. 点停止,将当前时间显示在textview上
      3. 显示满3个后,再次点击停止,清除原来第一个记录,整体向前移一个textview
      4. 重置,清空3个记录
    

返回顶部


♦ 设置 activity_time_recoder.xml 界面

  • 与计时器界面相比较,改变的是下面显示出每次暂停时,记录的时间。

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.TimeCountActivity"
    android:background="@drawable/main_page">


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="176dp"
        android:layout_marginLeft="176dp"
        android:layout_marginTop="100dp"
        android:text="秒表记录"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textColor="@android:color/white"/>

    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="129dp"
        android:layout_marginLeft="129dp"
        android:layout_marginTop="183dp"
        android:layout_marginBottom="98dp"
        android:text="01:00"
        android:textColor="@android:color/white"
        android:textSize="60dp" />

    <Button
        android:id="@+id/begin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginBottom="240dp"
        android:text="开始" />

    <Button
        android:id="@+id/stop1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="161dp"
        android:layout_marginRight="161dp"
        android:layout_marginBottom="240dp"
        android:text="暂停" />

    <Button
        android:id="@+id/restart1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginBottom="240dp"
        android:text="重置" />

    <TextView
        android:id="@+id/record1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="52dp"
        android:layout_marginLeft="52dp"
        android:layout_marginBottom="179dp"
        android:hint="记录1"
        android:text="00:00"
        android:textColor="@android:color/white"
        android:textSize="19dp" />

    <TextView
        android:id="@+id/record2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="179dp"
        android:hint="记录2"
        android:text="00:00"
        android:textColor="@android:color/white"
        android:textSize="19dp" />

    <TextView
        android:id="@+id/record3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="53dp"
        android:layout_marginRight="53dp"
        android:layout_marginBottom="179dp"
        android:hint="记录3"
        android:text="00:00"
        android:textColor="@android:color/white"
        android:textSize="19dp" />

</RelativeLayout>

返回顶部


♦ 编写 MyToolActivity 实现功能界面跳转

  • MyTool界面添加秒表记录按钮组件
 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="250dp"
        android:layout_marginEnd="39dp"
        android:layout_marginRight="39dp"
        android:text="秒表记录" />

在这里插入图片描述

  • 编写MyToolActivity 实现功能界面跳转
public class MyToolActivity extends AppCompatActivity {
    
    

    TextView welcome;
    Button net, call, quick_call,flashlight,timecount,timerecoder;

    //权限请求:载入界面、特定情况触发
    //定义权限 --- 动态权限,申请哪一个就启动哪一个,通常为常量
    private static String[] PERMISSIONS_STORAGE = {
    
    
            Manifest.permission.CALL_PHONE
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_tool);

        // 欢迎信息
        welcome = findViewById(R.id.welcome);
        final Intent intent = getIntent();
        intent.getStringExtra("username");
        welcome.setText("欢迎您" + intent.getStringExtra("username"));

        // 获取工具组件
        net = findViewById(R.id.net);
        call = findViewById(R.id.call);
        quick_call = findViewById(R.id.quick_call);
        flashlight = findViewById(R.id.flashlight);
        timecount = findViewById(R.id.timecount);
        timerecoder = findViewById(R.id.timerecoder);

        ...........................
        
        // 秒表记录
        timerecoder.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                // 创建页面跳转
                Intent intent = new Intent();
                intent.setClass(MyToolActivity.this,TimeRecoderActivity.class);
                startActivity(intent);
            }
        });
    }
}

返回顶部


♦ 编写TimeRecoderActivity实现功能

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import com.example.myapplication_test.R;

import java.util.ArrayList;

public class TimeRecoderActivity extends AppCompatActivity {
    
    

    // 创建组件对象
    Button begin, stop, restart;
    Chronometer chronometer; // 计时器
    Vibrator vibrator; // 手机震动
    long waitTime; // 流失的时间

    TextView recoder1, recoder2, recoder3;   // 记录
    TextView[] recoders = new TextView[]{
    
    recoder1, recoder2, recoder3};
    int times = 0; // 记录stop的次数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_recoder);

        // 获取组件
        begin = findViewById(R.id.begin1);
        stop = findViewById(R.id.stop1);
        restart = findViewById(R.id.restart1);
        chronometer = findViewById(R.id.chronometer1);
        // 获取震动
        vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        // 记录
        recoders[0] = findViewById(R.id.record1);
        recoders[1] = findViewById(R.id.record2);
        recoders[2] = findViewById(R.id.record3);

        // begin 开始时可以停止,重置、开始不可点
        begin.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                // 计时开始
                chronometer.start();
                // 设置按钮
                begin.setEnabled(false);
                stop.setEnabled(true);
                restart.setEnabled(false);
                // 开始后,如果点了暂停,还要接着计时,就“继续”
                begin.setText("继续");
                // 实际上计时器上显示的时间
                // SystemClock.elapsedRealtime() 返回系统启动到现在的毫秒数,包含休眠时间。
                // chronometer.setBase() 设置计时器的时间,一开始waitTime=0,所以就是现实真实的计时时间
                chronometer.setBase(SystemClock.elapsedRealtime() - waitTime);
            }
        });
        // stop 停止时,可以继续,也可以重置,但不可以再次停止
        stop.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                // 暂停计时
                chronometer.stop();
                // 设置按钮
                begin.setEnabled(true);
                stop.setEnabled(false);
                restart.setEnabled(true);
                // 暂停后,计时器停止,浪费的时间 = 下一次操作开始(系统启动到现在的时间毫秒数) - 暂停时计时器上的时间
                waitTime = SystemClock.elapsedRealtime() - chronometer.getBase();
                // 处理计时记录
                times++;
                if (times > 3) {
    
    
                    recoders[0].setText(recoders[1].getText());
                    recoders[1].setText(recoders[2].getText());
                    recoders[2].setText(chronometer.getText());
                } else {
    
    
                    recoders[times-1].setText(chronometer.getText());
                }
            }
        });
        // 重置后只能开始
        restart.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                // 重新开始时,浪费的时间归零,需要重新计算
                waitTime = 0;
                // 设置按钮
                begin.setEnabled(true);
                begin.setText("开始");
                stop.setEnabled(false);
                restart.setEnabled(false);
                // 计时器复位
                chronometer.setBase(SystemClock.elapsedRealtime());
                // 重置记录、stop次数
                times = 0;
                for (int i = 0; i < 3; i++) {
    
    
                    recoders[i].setText("00:00");
                }

            }
        });
        // 添加震动功能
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    
    
            @Override
            public void onChronometerTick(Chronometer chronometer) {
    
    
                // 获取计时器上的字符串
                String time = chronometer.getText().toString();
                // 将字符截取并转为int类型数值计算成秒
                int newTime = Integer.parseInt(time.substring(0, time.indexOf(":"))) * 60 +
                        Integer.parseInt(time.substring(time.length() - 2));
                // 每整十秒震动一次,一次0.5秒
                if (newTime % 10 == 0 && newTime != 0) {
    
    
                    vibrator.vibrate(new long[]{
    
    0, 500}, -1);  // 震动一下0.5秒 -1代表不重复
                }
            }
        });

    }
}

返回顶部


♦ 检测

在这里插入图片描述

返回顶部


上一篇:MyTool工具界面:秒表                                                 下一篇:MyTool工具界面:便签

猜你喜欢

转载自blog.csdn.net/qq_45797116/article/details/114107291