安卓開發實現雙方比賽計時器
阿新 • • 發佈:2018-12-22
安卓開發,實現雙方比賽計時器:
效果圖:
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>
目前還只是比較簡單的功能,感興趣的小夥伴可以繼續在此基礎上進行開發。謝謝