Timer的用法和PorgressBar

Timer 的用法

Timer要和TimerTasker一起使用,Timer设置了执行的时间和周期,TimerTasker创建线程执行具体的任务.

启动Timer

 Timer timer = new Timer();
 TimerTasker timerTask = new TimerTask()
 {  
  @Override
public void run()
{
//被周期性执行的任务  
}
};

// 启动任务 ,延时1秒,每1秒执行一次task的run方法
timer.schedule(timerTask, 1000, 1000);


结束任务

if (timer != null)
{
timerTask.cancel();
timer.cancel();
timerTask = null;
timer = null;
}


一般来说我在onResume时让Timer重新启动,而在onPause时让Timer终止.


PorgressBar 有两种,圆形和条形

圆形没有进度值,就一直转

条形的有最大值和进度值


当然原型的有大中小三种风格,条形的有水平和垂直两种风格

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

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

条形的控制:

首先要设置最大值 

bar3.setMax(100);

其次根据实际进度设置值

bar3.setProgress(progress);



package com.example.l01uicontrols;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.ProgressBar;

public class UsingProgressBar extends Activity
{

	@Override
	protected void onPause()
	{
		stopTimer();
		super.onPause();
	}

	@Override
	protected void onResume()
	{
		startTimer();
		super.onResume();
	}

	private ProgressBar bar1, bar2, bar3;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.using_progress_bar);
		bar3 = (ProgressBar) findViewById(R.id.progressBar3);
		bar3.setMax(100);

	}

	private Timer timer = null;
	private TimerTask timerTask = null;

	public void startTimer()
	{
		if (timer == null)
		{
			timer = new Timer();
			timerTask = new TimerTask()
			{
				private int progress = 0;

				@Override
				public void run()
				{
					if (progress != 100)
					{
						progress += 10;
						bar3.setProgress(progress);
					}
					else
					{
						stopTimer();
					}
				}
			};

			// 启动任务 ,延时1秒,每1秒执行一次task的run方法
			timer.schedule(timerTask, 1000, 1000);

		}

	}

	public void stopTimer()
	{
		if (timer != null)
		{
			timerTask.cancel();
			timer.cancel();
			timerTask = null;
			timer = null;
		}

	}

}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值