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

标签 android timer countdown progress

嗨,有人可以帮助我完成我的小项目吗,我一直在学习本教程,我到达了将 1 分钟插入 EditText 的部分,进度条工作正常,每秒 1 个进度,但是当我输入超过进入 EditText 1 分钟后,进度条不起作用。它没有下降请帮助?

主.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="#086A87">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp" >

    <EditText
        android:id="@+id/edtTimerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="minutes"
        android:inputType="phone" />

    <Button
        android:id="@+id/btnStartTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="Start Timer" 
        android:background="@drawable/custombuttongreen"
        android:textColor="#fff"/>

    <Button
        android:id="@+id/btnStopTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="Stop Timer"
        android:visibility="gone" 
        android:background="@drawable/custombuttongreen"
        android:textColor="#fff"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

</LinearLayout>



<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"  >

    <ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="350dip"
    android:layout_height="350dip"
    android:indeterminate="false"
    android:progressDrawable="@drawable/circle"
    android:background="@drawable/circle_shape"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="60"
    android:progress="0" />

    <TextView
    android:id="@+id/tvTimeCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="00:00" 
    android:textColor="#fff"
    android:textSize="60dip"/>

</RelativeLayout>

主 Activity .java

package com.tag.countdowntimer;
import com.tag.countdowntimer.R.drawable;
import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

int i=-1;
ProgressBar mProgressBar;

private Button buttonStartTime, buttonStopTime;
private EditText edtTimerValue;
private TextView textViewShowTime; // will show the time
private CountDownTimer countDownTimer; // built in android class
                                        // CountDownTimer
private long totalTimeCountInMilliseconds; // total count down time in
                                            // milliseconds
private long timeBlinkInMilliseconds; // start time of start blinking
private boolean blink; // controls the blinking .. on and off

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonStartTime = (Button) findViewById(R.id.btnStartTime);
    buttonStopTime = (Button) findViewById(R.id.btnStopTime);
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);

    buttonStartTime.setOnClickListener(this);
    buttonStopTime.setOnClickListener(this);

    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnStartTime) {
        textViewShowTime.setTextAppearance(getApplicationContext(),
                R.style.normalText);
        setTimer();

        //Hides the Keyboard
        InputMethodManager imm = (InputMethodManager)getSystemService(
                  Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edtTimerValue.getWindowToken(), 0);

        buttonStopTime.setVisibility(View.VISIBLE);
        buttonStartTime.setVisibility(View.GONE);
        edtTimerValue.setVisibility(View.GONE);
        edtTimerValue.setText("");
        //textViewShowTime.setTextColor(color.white);
        //textViewShowTime.getContext();
        startTimer();

    } else if (v.getId() == R.id.btnStopTime) {
        countDownTimer.cancel();
        buttonStartTime.setVisibility(View.VISIBLE);
        buttonStopTime.setVisibility(View.GONE);
        edtTimerValue.setVisibility(View.VISIBLE);
    }
}

private void setTimer() {
    int time = 0;
    if (!edtTimerValue.getText().toString().equals("")) {
        time = Integer.parseInt(edtTimerValue.getText().toString());
    } else
        Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                Toast.LENGTH_LONG).show();

    totalTimeCountInMilliseconds = 60 * time * 1000;

    timeBlinkInMilliseconds = 30 * 1000;
}

private void startTimer() {
    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            long seconds = leftTimeInMilliseconds / 1000;
            //i++;
            //Setting the Progress Bar to decrease wih the timer
            mProgressBar.setProgress((int) (leftTimeInMilliseconds / 1000));
            textViewShowTime.setTextAppearance(getApplicationContext(),
                    R.style.normalColor);


            if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
                textViewShowTime.setTextAppearance(getApplicationContext(),
                        R.style.blinkText);
                // change the style of the textview .. giving a red
                // alert style

                if (blink) {
                    textViewShowTime.setVisibility(View.VISIBLE);
                    // if blink is true, textview will be visible
                } else {
                    textViewShowTime.setVisibility(View.INVISIBLE);
                }

                blink = !blink; // toggle the value of blink
            }

            textViewShowTime.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));
            // format the textview to show the easily readable format

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
            textViewShowTime.setText("Time up!");
            textViewShowTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);
            edtTimerValue.setVisibility(View.VISIBLE);
        }

    }.start();

}
}

定时器的正常状态

enter image description here

当我输入 1 分钟

enter image description here

当我进入3分钟

enter image description here

进度条没有倒计时

最佳答案

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

为了让您的进度条始终正常工作,在您的“setTimer()”方法中写入以下行,而不是这样。

mProgressBar.setMax(60*time);

关于Android倒数计时器循环进度条与计时器不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631817/

相关文章:

android - 在线程内释放对象时出错

java - 在 Android 应用程序中添加许多已保存的图像

android - 设置 QTimer 间隔时 Qt 应用程序崩溃

java - 如何使用 sleep /等待功能来显示倒计时

java - 将 PDF 文件附加到来自应用程序的电子邮件

java - 如何从 Android 中的 ArrayList 中删除项目

c# - 使用计时器/任务/后台 worker 的数据采集循环

c# - 延迟按键操作中的搜索功能

javascript - 将秒输出更改为分钟和小时

jQuery 倒计时 Keith Wood 无法在 mozilla 中工作