//一个Button 控件 一个 ProgressBar控件
一、res/layout布局界面android
代码ide
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>布局
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
style="@android :style/Widget.ProgressBar.Horizontal"
/>
<!-- 在Button 里面设置一个 onClick 监听 -->
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="开始"
android:textSize="50sp"
android:onClick="start"
/>this
</LinearLayout>.net
---------------------------------------xml
二、而后在MainActivity 里面 实现功能对象
代码进程
public class MainActivity extends Activity {
private boolean isstart ;
private ProgressBar pb;
//建立一个Timer的对象 用来控制 ProgressBar进度
private Timer timer = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) this.findViewById(R.id.progressbar);
//设置ProgressBar 有多长
pb.setMax(100);
}get
//监听 的方法
public void start(View view){
pb.setProgress(0);//设置 刻度 从0开始
//每隔 一段时间 更新 进度
//计时器从 0 开始 100毫秒 更新 一次
timer.schedule(new MyTask(), 0, 100);
isstart = true;
}
public class MyTask extends TimerTask{it
@Override public void run() { // TODO Auto-generated method stub //获取 进度条 的当前 进度 int currentprogress = pb.getProgress(); if(currentprogress == pb.getMax()){ this.cancel(); //中止 进程 isstart = false; return ; }//进度条 按计时器 100毫秒 更新 一次 只加1即 currentprogress++ currentprogress++; pb.setProgress(currentprogress); //设置进程的 进度 } }}