Android开发经验总结篇Android知识

自定义倒计时

2018-02-27  本文已影响7人  zhengLH

【需求】可以自定义 分钟,秒数
【截图】

时间.png

【思想】从大到小: 先拆分 分钟, 再拆分秒

 (1)将时间  根据分钟  分为 (A)大于1min 和  (B)小于1min
   (A)再根据秒  分为 
   (A.1) 等于0 second:     min -1,  且second 重置为 59
      (A.2)   不等于0 second :   second -1
             (A.1)注意: 区分 分钟 秒 的 单位数 和 双位数
               (A.2)    同上

   (B)根据second 分为  
      (B.1)second = 0 和 (B.2)second 不等于0 
            (B.1)  结束。移除 时间任务
         (B.2)区分 分钟 秒 的 单位数 和 双位数

【完整代码】

public class CountDownActivity extends AppCompatActivity {

private int minute = 1;  //这是分钟
private int second = 0;  //这是分钟后面的秒数。这里是以30分钟为例的,所以,minute是30,second是0
private TextView tvname;
private Timer timer;
private TimerTask timerTask; //这是接收回来处理的消息

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        if (minute == 0) {
            if (second == 0) {
                tvname.setText("结束");
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                if (timerTask != null) {
                    timerTask = null;
                }
            } else {
                second--;
                if (second >= 10) {
                    tvname.setText("0" + minute + ":" + second);
                } else {
                    tvname.setText("0" + minute + ":0" + second);
                }
            }
        } else {
            if (second == 0) {
                second = 59;
                minute--;
                if (minute >= 10) {
                    tvname.setText(minute + ":" + second);
                } else {
                    tvname.setText("0" + minute + ":" + second);
                }
            } else {
                second--;
                if (second >= 10) {
                    if (minute >= 10) {
                        tvname.setText(minute + ":" + second);
                    } else {
                        tvname.setText("0" + minute + ":" + second);
                    }
                } else {
                    if (minute >= 10) {
                        tvname.setText(minute + ":0" + second);
                    } else {
                        tvname.setText("0" + minute + ":0" + second);
                    }
                }
            }
        }
    }
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_count_down);
    initView();
}

private void initView() {

    tvname = (TextView) findViewById(R.id.tvname);
    if (minute == 0 && second == 30) {
        tvname.setText("" + minute + ":" + second);
        tvname.setTextColor(getResources().getColor(R.color.font_deep_red));
    } else {
        tvname.setText(minute + ":" + second);
    }


    // 计时器
    timerTask = new TimerTask() {
        @Override
        public void run() {
            Message msg = new Message();
            msg.what = 0;
            handler.sendMessage(msg);
        }
    };
    timer = new Timer();
    timer.schedule(timerTask, 0, 1000);  // 开启 计时任务
}

@Override
protected void onDestroy() {
    if (timer != null) {
        timer.cancel();
        timer = null;
    }
    if (timerTask != null) {
        timerTask = null;
    }
    minute = -1;
    second = -1;
    super.onDestroy();
}

}

【xml布局】

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tvname"
    android:textSize="@dimen/textSize_22sp"
    android:background="@drawable/bg_darkorange_round"
    android:textColor="@color/colorWhite"
    android:padding="@dimen/padding_10dp"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

   </RelativeLayout>
上一篇下一篇

猜你喜欢

热点阅读