安卓倒数计时器通知进度条不与定时器匹配 [英] Android Countdown Timer Circular Progress Bar doesn't match with timer

查看:949
本文介绍了安卓倒数计时器通知进度条不与定时器匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨谁能帮助我在我的小项目,请,我一直跟随本教程中,我得到了我的插入1分钟到EditText上的进度条工作每秒罚款1进展的一部分,但是当我把多1分钟到EditText上的进度条不工作。它不会变为Down,请帮助?

Hi can anyone help me on my small project please, I have been following this Tutorial and I got to the part where I insert 1 Minute into the EditText the Progress Bar works fine 1 Progress per sec but when I put in more than 1 Minute into the EditText the Progress Bar does not work. It does not goes down please help?

main.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>

MainActivity.java

MainActivity.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();

}
}

定时器正常状态

当我进入1分钟

当我进入3分钟

进度栏未开盘

推荐答案

在你的main.xml中,对进度你为60.提到的最大值所以进度条采取它最大值为60,你的进度条开始从60下降秒起实施。

In your main.xml, for ProgressBar you mentioned max value as 60. So the progress bar takes it max value as 60 and your progress bar starts decreasing from 60 seconds onwards.

相反的,为了你的工作​​进度栏正确任何时候,在你的SetTimer的()方法编写下面一行。

Instead of that to work your Progress bar properly all times, in your "setTimer()" method write the below line.

mProgressBar.setMax(60*time);

这篇关于安卓倒数计时器通知进度条不与定时器匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆