安卓开发,实现双方比赛计时器:
效果图:
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_timer_main_activity);
this.initComponents();
this.initThreads();
leftLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
@Override
public 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() {
@Override
public void onClick(View view) {
Intent settingIntent = new Intent(TimerMainActivity.this, SettingActivity.class);
startActivityForResult(settingIntent, 1);
}
});
leftLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
endCurrentPlay();
return true;
}
});
rightLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public 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() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(TimerMainActivity.this, "您点击了取消结束本局", Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public 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
*/
@Override
protected 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);
}
@Override
protected 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());
}
@Override
public 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_timer_setting_activity);
this.initComponents();
this.settingCertainBtn.setOnClickListener(new View.OnClickListener() {
@Override
public 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"?>
<LinearLayout
xmlns: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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="horizontal">
<LinearLayout
android: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">
<TextView
android: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>
<ProgressBar
android: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"
/>
<TextView
android: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>
<LinearLayout
android: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">
<TextView
android: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>
<ProgressBar
android: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"
/>
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android: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="进度条测试"/>-->
<Button
android: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">
<LinearLayout
android: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">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="时:"
/>
<EditText
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分:"
/>
<EditText
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="秒:"
/>
<EditText
android: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>
<Button
android: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>
目前还只是比较简单的功能,感兴趣的小伙伴可以继续在此基础上进行开发。谢谢