Android倒计时计时器循环进度条与计时器不匹配

前端之家收集整理的这篇文章主要介绍了Android倒计时计时器循环进度条与计时器不匹配前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,任何人都可以帮助我完成我的小项目,我一直在关注这个教程,我到达了我在EditText中插入1分钟的部分,进度条工作正常1每秒进度但是当我输入超过1分钟时EditText进度条不起作用.它没有下降请帮忙吗?

main.xml中

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:background="#086A87">
  8.  
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center"
  13. android:orientation="horizontal"
  14. android:padding="10dp" >
  15.  
  16. <EditText
  17. android:id="@+id/edtTimerValue"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:ems="10"
  22. android:hint="minutes"
  23. android:inputType="phone" />
  24.  
  25. <Button
  26. android:id="@+id/btnStartTime"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_margin="5dp"
  30. android:layout_weight="2"
  31. android:gravity="center"
  32. android:text="Start Timer"
  33. android:background="@drawable/custombuttongreen"
  34. android:textColor="#fff"/>
  35.  
  36. <Button
  37. android:id="@+id/btnStopTime"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:layout_margin="5dp"
  41. android:layout_weight="2"
  42. android:gravity="center"
  43. android:text="Stop Timer"
  44. android:visibility="gone"
  45. android:background="@drawable/custombuttongreen"
  46. android:textColor="#fff"/>
  47.  
  48. </LinearLayout>
  49.  
  50. <LinearLayout
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:gravity="center"
  54. android:orientation="vertical" >
  55.  
  56. </LinearLayout>
  57.  
  58.  
  59.  
  60. <RelativeLayout
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content" >
  63.  
  64. <ProgressBar
  65. android:id="@+id/progressbar"
  66. android:layout_width="350dip"
  67. android:layout_height="350dip"
  68. android:indeterminate="false"
  69. android:progressDrawable="@drawable/circle"
  70. android:background="@drawable/circle_shape"
  71. style="?android:attr/progressBarStyleHorizontal"
  72. android:max="60"
  73. android:progress="0" />
  74.  
  75. <TextView
  76. android:id="@+id/tvTimeCount"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_centerHorizontal="true"
  80. android:layout_centerVertical="true"
  81. android:text="00:00"
  82. android:textColor="#fff"
  83. android:textSize="60dip"/>
  84.  
  85. </RelativeLayout>

MainActivity.java

  1. package com.tag.countdowntimer;
  2. import com.tag.countdowntimer.R.drawable;
  3. import android.R.color;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.os.CountDownTimer;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.inputmethod.InputMethodManager;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ProgressBar;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. public class MainActivity extends Activity implements OnClickListener {
  18.  
  19. int i=-1;
  20. ProgressBar mProgressBar;
  21.  
  22. private Button buttonStartTime,buttonStopTime;
  23. private EditText edtTimerValue;
  24. private TextView textViewShowTime; // will show the time
  25. private CountDownTimer countDownTimer; // built in android class
  26. // CountDownTimer
  27. private long totalTimeCountInMilliseconds; // total count down time in
  28. // milliseconds
  29. private long timeBlinkInMilliseconds; // start time of start blinking
  30. private boolean blink; // controls the blinking .. on and off
  31.  
  32. /** Called when the activity is first created. */
  33. @Override
  34. public void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.main);
  37.  
  38. buttonStartTime = (Button) findViewById(R.id.btnStartTime);
  39. buttonStopTime = (Button) findViewById(R.id.btnStopTime);
  40. textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
  41. edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);
  42.  
  43. buttonStartTime.setOnClickListener(this);
  44. buttonStopTime.setOnClickListener(this);
  45.  
  46. mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
  47.  
  48. }
  49.  
  50. @Override
  51. public void onClick(View v) {
  52. if (v.getId() == R.id.btnStartTime) {
  53. textViewShowTime.setTextAppearance(getApplicationContext(),R.style.normalText);
  54. setTimer();
  55.  
  56. //Hides the Keyboard
  57. InputMethodManager imm = (InputMethodManager)getSystemService(
  58. Context.INPUT_METHOD_SERVICE);
  59. imm.hideSoftInputFromWindow(edtTimerValue.getWindowToken(),0);
  60.  
  61. buttonStopTime.setVisibility(View.VISIBLE);
  62. buttonStartTime.setVisibility(View.GONE);
  63. edtTimerValue.setVisibility(View.GONE);
  64. edtTimerValue.setText("");
  65. //textViewShowTime.setTextColor(color.white);
  66. //textViewShowTime.getContext();
  67. startTimer();
  68.  
  69. } else if (v.getId() == R.id.btnStopTime) {
  70. countDownTimer.cancel();
  71. buttonStartTime.setVisibility(View.VISIBLE);
  72. buttonStopTime.setVisibility(View.GONE);
  73. edtTimerValue.setVisibility(View.VISIBLE);
  74. }
  75. }
  76.  
  77. private void setTimer() {
  78. int time = 0;
  79. if (!edtTimerValue.getText().toString().equals("")) {
  80. time = Integer.parseInt(edtTimerValue.getText().toString());
  81. } else
  82. Toast.makeText(MainActivity.this,"Please Enter Minutes...",Toast.LENGTH_LONG).show();
  83.  
  84. totalTimeCountInMilliseconds = 60 * time * 1000;
  85.  
  86. timeBlinkInMilliseconds = 30 * 1000;
  87. }
  88.  
  89. private void startTimer() {
  90. countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds,500) {
  91. // 500 means,onTick function will be called at every 500
  92. // milliseconds
  93.  
  94. @Override
  95. public void onTick(long leftTimeInMilliseconds) {
  96. long seconds = leftTimeInMilliseconds / 1000;
  97. //i++;
  98. //Setting the Progress Bar to decrease wih the timer
  99. mProgressBar.setProgress((int) (leftTimeInMilliseconds / 1000));
  100. textViewShowTime.setTextAppearance(getApplicationContext(),R.style.normalColor);
  101.  
  102.  
  103. if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
  104. textViewShowTime.setTextAppearance(getApplicationContext(),R.style.blinkText);
  105. // change the style of the textview .. giving a red
  106. // alert style
  107.  
  108. if (blink) {
  109. textViewShowTime.setVisibility(View.VISIBLE);
  110. // if blink is true,textview will be visible
  111. } else {
  112. textViewShowTime.setVisibility(View.INVISIBLE);
  113. }
  114.  
  115. blink = !blink; // toggle the value of blink
  116. }
  117.  
  118. textViewShowTime.setText(String.format("%02d",seconds / 60)
  119. + ":" + String.format("%02d",seconds % 60));
  120. // format the textview to show the easily readable format
  121.  
  122. }
  123.  
  124. @Override
  125. public void onFinish() {
  126. // this function will be called when the timecount is finished
  127. textViewShowTime.setText("Time up!");
  128. textViewShowTime.setVisibility(View.VISIBLE);
  129. buttonStartTime.setVisibility(View.VISIBLE);
  130. buttonStopTime.setVisibility(View.GONE);
  131. edtTimerValue.setVisibility(View.VISIBLE);
  132. }
  133.  
  134. }.start();
  135.  
  136. }
  137. }

正常的定时器状态

当我输入1分钟

当我进入3分钟

进度条不是倒计时

解决方法

在main.xml中,对于ProgressBar,您提到最大值为60.因此,进度条将最大值设为60,进度条从60秒开始逐渐减少.

而不是始终正确地处理您的进度条,在“setTimer()”方法中写下面的行.

  1. mProgressBar.setMax(60*time);

猜你在找的Android相关文章