安卓开发,实现双方比赛计时器:

效果图:

TimerMainActivity.java

package com.example.fujianping.httprequest01.mytimer;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;import com.example.fujianping.httprequest01.R;public class TimerMainActivity extends Activity {private final static String TAG = "TimerMainActivity";private LinearLayout leftLinearLayout;private LinearLayout rightLinearLayout;private TextView leftTimerTv;private TextView rightTimerTv;private Button settingBtn;private TextView leftTipTv;private TextView rightTipTv;private ProgressBar leftProgressBar;private ProgressBar rightProgressBar;private long seconds = 1800;private MyThread myThread01;private MyThread myThread02;private boolean leftState = true;private boolean rightState = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_timer_main_activity);this.initComponents();this.initThreads();leftLinearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (settingBtn.getVisibility() == View.VISIBLE) {settingBtn.setVisibility(View.GONE);}if (leftState) {myThread02.startTimer();myThread01.pauseTimer();leftState = false;rightState = true;} else {Toast.makeText(TimerMainActivity.this, "对方未操作,您不可连续操作!", Toast.LENGTH_SHORT).show();}}});rightLinearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (settingBtn.getVisibility() == View.VISIBLE) {settingBtn.setVisibility(View.GONE);}if (rightState) {myThread01.startTimer();myThread02.pauseTimer();leftState = true;rightState = false;} else {Toast.makeText(TimerMainActivity.this, "对方未操作,您不可连续操作!", Toast.LENGTH_SHORT).show();}}});settingBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent settingIntent = new Intent(TimerMainActivity.this, SettingActivity.class);startActivityForResult(settingIntent, 1);}});leftLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {endCurrentPlay();return true;}});rightLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {endCurrentPlay();return true;}});//        this.getDynamicTimerText();}/*** 获取动态时间信息,以便当时间为0后,即结束本局,分出胜负*/
//    public void getDynamicTimerText() {
//        String leftTimer = leftTimerTv.getText().toString();
//        Log.d(TAG, "getDynamicTimerText() leftTimer = " + leftTimer);
//        String rightTimer = leftTimerTv.getText().toString();
//        Log.d(TAG, "getDynamicTimerText() rightTimer = " + rightTimer);
//    }/*** 长按结束本局*/public void endCurrentPlay() {AlertDialog.Builder builder = new AlertDialog.Builder(TimerMainActivity.this);builder.setTitle("温馨提示").setMessage("是否结束本局?").setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(TimerMainActivity.this, "您点击了取消结束本局", Toast.LENGTH_LONG).show();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(TimerMainActivity.this, "您点击了确定结束本局", Toast.LENGTH_LONG).show();myThread01.pauseTimer();myThread02.pauseTimer();settingBtn.setVisibility(View.VISIBLE);}}).create().show();}/*** 获取设置好的时长* @param requestCode* @param resultCode* @param data*/@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case 1:if (resultCode == RESULT_OK) {seconds = data.getLongExtra("totalSeconds", 1800);myThread01.setTimeText(seconds);myThread02.setTimeText(seconds);myThread01.setSeconds(seconds);myThread02.setSeconds(seconds);Util.transportTime(leftTipTv, seconds);Util.transportTime(rightTipTv, seconds);leftProgressBar.setMax((int) seconds + 1);Log.d(TAG, "onActivityResult() leftProgressBar.getMax() = " + leftProgressBar.getMax());rightProgressBar.setMax((int) seconds + 1);Log.d(TAG, "onActivityResult() rightProgressBar.getMax() = " + rightProgressBar.getMax());}break;default:break;}}public void initThreads() {myThread01 = new MyThread(leftTimerTv, leftProgressBar);myThread01.setTimeText(seconds);myThread02 = new MyThread(rightTimerTv, rightProgressBar);myThread02.setTimeText(seconds);}public void initComponents() {leftLinearLayout = findViewById(R.id.left);rightLinearLayout = findViewById(R.id.right);leftTimerTv = (TextView) findViewById(R.id.left_timer_tv);rightTimerTv = (TextView) findViewById(R.id.right_timer_tv);settingBtn = (Button) findViewById(R.id.setting_btn);leftTipTv = (TextView) findViewById(R.id.left_tip_tv);rightTipTv = (TextView) findViewById(R.id.right_tip_tv);leftProgressBar = (ProgressBar) findViewById(R.id.left_progressbar);leftProgressBar.setMax((int) seconds + 1);rightProgressBar = (ProgressBar) findViewById(R.id.right_progressbar);rightProgressBar.setMax((int) seconds + 1);}@Overrideprotected void onDestroy() {super.onDestroy();this.myThread01 = null;this.myThread02 = null;}
}

线程类:MyThread.java 

package com.example.fujianping.httprequest01.mytimer;import android.os.Handler;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;import com.example.fujianping.httprequest01.R;/*** Created by Administrator on 2018/12/22.*/public class MyThread implements Runnable {private final static String TAG = "MyThreadTest01";private Handler handler = new Handler();private long seconds = 1800;private TextView textView;private ProgressBar progressBar;public MyThread(TextView textView, ProgressBar progressBar) {this.textView = textView;this.progressBar = progressBar;}public void setSeconds(long seconds) {this.seconds = seconds;this.progressBar.setMax((int) seconds + 1);Log.d(TAG, "setSeconds(): progressBar.getMax() = " + progressBar.getMax());}@Overridepublic void run() {this.setTimeText(seconds--);
//        progressBar.setMax((int) seconds--);
//        progressBar.setProgress((int)seconds--);this.setProgressBar(seconds);handler.postDelayed(this, 1000);}public void setProgressBar(long seconds) {int totalSeconds = (int) seconds;
//        this.progressBar.setProgress(totalSeconds);Log.d(TAG, "setProgressBar(): totalSeconds = " + totalSeconds);
//        Log.d(TAG, "setProgressBar(): progressBar.getProgress() = " + progressBar.getProgress());if(totalSeconds >= 0){// 设置主进度条的当前值progressBar.setProgress(totalSeconds);// 设置第二进度条的当前值progressBar.setSecondaryProgress(totalSeconds--);// 默认的进度条是无法显示进行的状态的}}public void setTimeText(long seconds) {
//        Log.d(TAG, "setTimeText() seconds = " + seconds);//当时间为0时,则对方胜出if (seconds < 0) {if (this.textView.getId() == R.id.left) {//为左边为0,则右边胜出handler.removeCallbacks(this);Log.d(TAG, "右方胜出");} else if (this.textView.getId() == R.id.right) {//为右边为0,则左边胜出handler.removeCallbacks(this);Log.d(TAG, "左方胜出");}} else {long time = seconds;//转换为秒int hour = (int) time / 3600;//时int minute = (int) time / 60 - hour * 60;//分int second = (int) time % 60;//秒textView.setText(String.format("%02d:%02d:%02d", hour, minute, second));}}public void startTimer() {handler.postDelayed(this, 0);}/*** 获取动态时间信息,以便当时间为0后,即结束本局,分出胜负*/public void getDynamicTimerText() {String timerTextValue = textView.getText().toString();Log.d(TAG, "getDynamicTimerText() leftTimer = " + timerTextValue);}public void pauseTimer() {handler.removeCallbacksAndMessages(null);}//    public interface TimerTextValueListener {
//        public long getTimerTextValue();
//    }
}

设置界面活动:SettingActivity .java

package com.example.fujianping.httprequest01.mytimer;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;import com.example.fujianping.httprequest01.R;public class SettingActivity extends Activity {private final static String TAG = "SettingActivity";private EditText hourEditText;private EditText minuteEditText;private EditText secondEditText;private Button settingCertainBtn;private int hour = 0;private int minute = 0;private int second = 0;private long totalSeconds;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_timer_setting_activity);this.initComponents();this.settingCertainBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getValues();Intent mainActivityIntent = new Intent();mainActivityIntent.putExtra("totalSeconds", totalSeconds);setResult(RESULT_OK, mainActivityIntent);finish();}});}public void getValues() {hour = hourEditText.getText().toString().equals("") ? 0 : Integer.parseInt(hourEditText.getText().toString());minute = minuteEditText.getText().toString().equals("") ? 0 : Integer.parseInt(minuteEditText.getText().toString());second = secondEditText.getText().toString().equals("") ? 0 : Integer.parseInt(secondEditText.getText().toString());//        second = Integer.parseInt(secondEditText.getText().toString());totalSeconds = hour * 3600 + minute * 60 + second;}public void initComponents() {hourEditText = (EditText) findViewById(R.id.hour_edit_text);minuteEditText = (EditText) findViewById(R.id.minute_edit_text);secondEditText = (EditText) findViewById(R.id.second_edit_text);settingCertainBtn = (Button) findViewById(R.id.setting_certain_btn);}
}

工具类 Util.java:只有一个方法:

package com.example.fujianping.httprequest01.mytimer;import android.widget.TextView;/*** Created by Administrator on 2018/12/22.*/public class Util {public static void transportTime(TextView textView, long seconds) {int hour = (int) seconds / 3600;//时int minute = (int) seconds / 60 - hour * 60;//分int second = (int) seconds % 60;//秒
//        textView.setText(String.format("%02d:%02d:%02d", hour, minute, second));textView.setText("总时长" + hour + "时" + minute + "分" + second + "秒");}//    public AlertDialog.Builder getDialog(final Context context, String title, String message) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//
//        return builder;
//    }}

布局文件:

主界面:my_timer_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/timer_main_activity"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.fujianping.httprequest01.mytimer.TimerMainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="5"android:orientation="horizontal"><LinearLayoutandroid:id="@+id/left"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="7dp"android:layout_weight="1"android:background="@color/colorLightBlue"android:orientation="vertical"><TextViewandroid:id="@+id/left_timer_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical|center_horizontal"android:layout_marginTop="50dp"android:text="00:00:60"android:textSize="40sp"></TextView><ProgressBarandroid:id="@+id/left_progressbar"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_width="250dp"android:layout_height="20dp"android:layout_gravity="center_horizontal"/><TextViewandroid:id="@+id/left_tip_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal|center_vertical"android:text="默认总时长30分钟"android:textSize="30sp"/></LinearLayout><LinearLayoutandroid:id="@+id/right"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="7dp"android:layout_weight="1"android:background="@color/colorLightGreen"android:orientation="vertical"><TextViewandroid:id="@+id/right_timer_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical|center_horizontal"android:layout_marginTop="50dp"android:text="00:00:60"android:textSize="40sp"></TextView><ProgressBarandroid:id="@+id/right_progressbar"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_width="250dp"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"/><TextViewandroid:id="@+id/right_tip_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal|center_vertical"android:text="默认总时长30分钟"android:textSize="30sp"></TextView></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="10dp"android:text="设置好时间后,任意一方点击即开始。比赛过程中,任意一方长按即结束此局。"android:textSize="15sp"/><!--<Button--><!--android:id="@+id/progressBar_btn"--><!--android:layout_width="wrap_content"--><!--android:layout_height="match_parent"--><!--android:layout_marginLeft="10dp"--><!--android:layout_marginRight="10dp"--><!--android:background="@color/colorLightRed"--><!--android:text="进度条测试"/>--><Buttonandroid:id="@+id/setting_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@color/colorLightRed"android:text="设置"/></LinearLayout></LinearLayout>

时间设置界面:my_timer_setting_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_setting"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.fujianping.httprequest01.mytimer.SettingActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginLeft="150dp"android:layout_marginRight="150dp"android:layout_marginTop="30dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="时:"/><EditTextandroid:id="@+id/hour_edit_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="5"android:inputType="numberSigned"android:maxLength="100"android:text="0"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginLeft="150dp"android:layout_marginRight="150dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="分:"/><EditTextandroid:id="@+id/minute_edit_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="5"android:inputType="numberSigned"android:maxLength="100"android:text="0"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginLeft="150dp"android:layout_marginRight="150dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="秒:"/><EditTextandroid:id="@+id/second_edit_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="5"android:inputType="numberSigned"android:maxLength="100"android:text="0"/></LinearLayout><Buttonandroid:id="@+id/setting_certain_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="150dp"android:layout_marginRight="150dp"android:layout_marginTop="50dp"android:background="@color/colorLightRed"android:text="确定"/></LinearLayout>

目前还只是比较简单的功能,感兴趣的小伙伴可以继续在此基础上进行开发。谢谢

安卓开发实现双方比赛计时器相关推荐

  1. 【大疆DJI】安卓开发实习历程- 0.前期准备到面试(HR电话初面+技术一面+技术二面/终面+OC)

    目录 前言 实习选择 0. 腾讯云 1. 面试复盘 2. 海投简历 大疆HR电话初面 大疆技术一面 0. 面试形式 1. 问题准备 2. 面试经过(70 mins) 大疆技术二面(终面) 0. 面试形 ...

  2. 我是如何学习安卓开发的

    我的安卓学习之路 我的安卓之路主要有四个阶段: 入门 实践 准备面试 工作 1.入门 2014 年,学习 MFC 中途放弃的我,偶然间看到 Mars 前辈的安卓视频,看了几天写了个简单的应用,觉得安卓 ...

  3. 安卓开发中非常炫的效果集合

    安卓开发中非常炫的效果集合 这几天开发的时候,想做一些好看而且酷炫的特效,于是又开始从网上收集各种特效资源.下面给大家一些我喜欢的把,附代码,喜欢的看源代码,然后加到自己项目去把!! 一个开源项目网站 ...

  4. Android安卓开发中图片缩放讲解

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  5. 如果成为一名高级安卓开发_什么是高级开发人员,我如何成为一名开发人员?

    如果成为一名高级安卓开发 Becoming a Senior Developer is something many of us strive for as we continue our code ...

  6. Android Studio安卓开发中使用json来作为网络数据传输格式

    如果你是在安卓开发中并且使用android studio,要使用json来作为数据传输的格式,那么下面是我的一些经验. 一开始我在android studio中导入那6个包,那6个包找了非常久,因为放 ...

  7. 从事安卓开发6年,我都有哪些收获?

    作者 | 拭心 来源 | 拭心又在思考了我的天(ID:evolution1024) 一转眼,我从事安卓开发工作已经六年有余,对安卓开发甚至软件开发的价值,每年都有更进一步的认识.对未来的方向,也从刚入 ...

  8. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  9. 安卓开发屏幕分辨率尺寸适配问题【原创】

    2019独角兽企业重金招聘Python工程师标准>>> 屏幕分辨率尺寸适配是安卓开发中的难题之一,我开发中的解决办法是: 1.多使用相对布局,即RelativeLayout,或者Li ...

最新文章

  1. BaseModelOutputWithPoolingAndCrossAttentions的API
  2. Bash的基础知识man手册
  3. 机器人学习--Turtelbot3学习-- Burger与waffle等版本的切换
  4. Error: listen EACCES 127.0.0.1
  5. Linux虚拟机安装应用程序提示Graphical installers are not supported by the vm
  6. 解决VirtualBox错误:“FATAL:No bootable medium found!”
  7. linux x64 ffmpeg,ffmpeg编译arm64动态包
  8. 为啥浏览器中的对象和w3c不一样??
  9. Flink报错:java.io.IOException: Insufficient number of network buffers
  10. Linux软件包安装和卸载
  11. zabbix 的安装
  12. VMwar配置静态ip
  13. 【三维装箱】基于matlab遗传和模拟退火算法求解三维装箱优化问题【含Matlab源码 031期】
  14. Onenote实现OCR识别图片
  15. 5 Openstack-Ussuri-Placement部署-ubuntu1804
  16. 上号神器扫码登录网站使用教程
  17. 京东商城禁止一淘网蜘蛛抓取内容
  18. 2018年,硅谷的P2P公司们为啥没跑路?
  19. 网络游戏是如何开发的?
  20. elastic APM 深入测试 一 (无嵌套调用的分布式微服务监控)

热门文章

  1. AAA企业信用评级的好处
  2. Swift函数式编程十二(表格应用)
  3. 传世引擎 A.F.T.Engine 完美破解步骤纪要
  4. 基于ADXL345的 LED摇摇棒原理
  5. Win10微软输入法自定义短语导入
  6. 国人对国产操作系统的误会
  7. 前端 Chrome 插件推荐
  8. win10或win11右键管理找不到文件解决
  9. 服务器维修合同样本,最新在线维修服务协议
  10. 单击选定单元格后输入新内容_Excel综合练习题